PowerQuery relative path solution

This article explains PowerQuery dynamic paths

Almost all Power BI users know how to get data from files or folders in Power BI Desktop, but here I propose a requirement that you don’t understand but may encounter. If the file or folder is a relative path instead of Absolute path, what to do?

For example, the report needs to obtain the specified data from the following path (assuming we cannot use Onedrive):

C:\Users\<your account name>\Documents

And this report needs to be shared with other report developers for secondary development. According to common practice, we will use:

File.Contents(<文件路径>)

to connect to the data source, but the path is static. When we share it with colleagues, the report should obtain the data source from the colleague's folder. If colleagues need to manually modify the path at this time, it will be very troublesome, especially when this When there are enough data sources. Therefore, is there a way to make the path dynamic? No matter we share it with anyone, the report can automatically obtain the specified data from the other party's folder.

In fact, this requirement has been mentioned in the official Power BI community , but so far there is no better solution, so I will explain the solution to this problem below, and there are multiple solutions.

Method 1: Path parameterization

Taking the above example as an example, if different users make the username part of the path different, then you only need to sort out all the computer usernames in the AD domain or on this machine, and then enter them when creating new parameters, such as naming them username. Then you can use the M statement to create a new custom function, where the path part is:

File.Contents("C:\Users\" & username & "\Documents")

Enter any username in the parameter, such as Jack, and then Invoke, you can piece together the user's path.

Insert image description here
Finally, we can save it as PBIT and share it with colleagues. Users only need to enter the path parameter when opening PBIT to load the specified data.

Method 2: Use Text.Contains

You can use this method if you don't want to use parameters and if different paths have the same parts. For example, for a file path, it might be located at:

C:\AAA\File\Project

May also be located at:

C:\BBB\File\Project

Maybe you will think that you can use method one to determine the path by asking the user to pass parameters, but here the name of the folder in our C drive directory is uncertain, random and irregular, or the parameter list is It is difficult to maintain manually. In this case, what should be done?

We know that it is troublesome for PowerQuery to use a for loop to traverse all paths and return the corresponding results like other programming, but we can use the Text.Contains function to filter out all the files containing the "File\Project" path on the C drive, as follows :

= Table.SelectRows(Source, each Text.Contains([Folder Path], "File\Project"))

In this way, we can get all relevant lists and then perform the next step of filtering. At this time, whoever opens this report can get the data they want.

other

Another scenario for this requirement is to obtain the latest file data in a certain folder. Suppose we have a folder with csv files with different names. We need to get the most recently modified csv file, but we don’t know which csv file with the same name we need to get. Then we can’t write a fixed csv file at this time. The absolute path comes, but we can use the Folder.Files function to get all the files under the path, and then use Table.FirstN to filter out the largest date in the Date created or Date modified field, which can perfectly solve the problem.

Guess you like

Origin blog.csdn.net/qq_44794714/article/details/110097231