Ravindra BagaleCourses & study guides Track your progress

Labs · Power BI

Lab: Turn the File Path into a Power Query Parameter So the Report Works on Any Laptop

Intermediate30 minPower BI Desktop (free) · pbi_sales.csv

Course: Power BI · Chapter 10: Parameters in Power Query (and Other Kinds of Parameters)

Chapter 10 explains Power Query parameters and other kinds of parameters; this lab builds the most useful one, a folder path.

Download pbi_sales.csv (24 orders)

Chala mitrano! "It works on my laptop" is the most famous sentence in IT, and also in Power BI. You send your report to a colleague and the refresh fails, because his file is in a different folder. Today we fix this forever with one parameter. One small box, and your report travels anywhere. Ek parameter, sagle laptop khush!

Suppose we are…

Suppose we are on the MIS team at Bajaj Finserv in Pune. Asha built a sales report on her laptop. The Source step says C:\Users\Asha\Downloads\pbi_sales.csv. She goes on leave and Rahul opens the report on his laptop. His Windows user is "Rahul" and he keeps files in a different folder, so Refresh fails. Rahul would have to open Power Query and edit every query by hand.

A parameter is a named value (like a small variable) that queries can use. If the folder lives in one parameter, Rahul changes one box and everything works. The sales data is sample data made up for practice.

Goal of this lab

By the end you will have:

  • A text parameter FolderPath.
  • A Source step that reads FolderPath & "pbi_sales.csv" instead of a fixed path.
  • Broken the refresh on purpose by moving the file, then fixed it with Edit parameters in seconds.
  • A template file (.pbit) that asks for the folder when someone opens it.

What you need (all free)

The data: before and after

Before. The path is typed inside the Source step. On another laptop the file is not found.

Before: Source step with File.Contents of C:\Users\Asha\Downloads\pbi_sales.csv; on Rahul's laptop the file cannot be found; the fix is editing every query

After. The folder lives in a parameter. Rahul changes only the parameter value.

After: parameter FolderPath C:\PowerBI\Labs\, Source step File.Contents(FolderPath & "pbi_sales.csv"), Rahul uses Edit parameters, result 24 rows and Sales 1,302,000 on both laptops

The formula

This is the only line of code we change. Before:

= Csv.Document(File.Contents("C:\PowerBI\Labs\pbi_sales.csv"), [Delimiter=",", Columns=7, Encoding=65001, QuoteStyle=QuoteStyle.None])

After:

= Csv.Document(File.Contents(FolderPath & "pbi_sales.csv"), [Delimiter=",", Columns=7, Encoding=65001, QuoteStyle=QuoteStyle.None])

& joins two pieces of text in M. If FolderPath is C:\PowerBI\Labs\, then FolderPath & "pbi_sales.csv" becomes C:\PowerBI\Labs\pbi_sales.csv, exactly the old path. That is why the parameter value must end with a backslash.

Steps

  1. Create the folder C:\PowerBI\Labs and save pbi_sales.csv in it.
  2. In Power BI Desktop: Get data → Text/CSV, pick C:\PowerBI\Labs\pbi_sales.csv, click Transform Data.
  3. In Applied Steps, click Source. If you do not see the formula bar above the table, tick View → Formula Bar.

    What you should see: the formula bar shows = Csv.Document(File.Contents("C:\PowerBI\Labs\pbi_sales.csv"), ….

  4. Click Home → Manage Parameters → New Parameter. Fill in: Name FolderPath, Type Text, Suggested Values Any value, Current Value C:\PowerBI\Labs\ (with the backslash at the end). Click OK.

    What you should see: FolderPath in the Queries list on the left, with a small parameter icon and the value C:\PowerBI\Labs.

  5. Click the pbi_sales query, then its Source step. In the formula bar, select the text "C:\PowerBI\Labs\pbi_sales.csv" (including the quotes) and replace it with FolderPath & "pbi_sales.csv". Press Enter.

    What you should see: the same 24 rows; no error. Click the last step in Applied Steps to check the final table is fine too.

  6. Click Home → Close & Apply. Add a Card with Sales.

    What you should see: 1.30M. Save the file as Lab-10-parameter.

  7. Now play Rahul. Create the folder C:\Users\Public\Documents\SalesData and move (cut and paste) pbi_sales.csv from C:\PowerBI\Labs into it. In Power BI click Home → Refresh.

    What you should see: an error like Could not find file 'C:\PowerBI\Labs\pbi_sales.csv'. Close the message.

  8. Click the small arrow under Home → Transform data and choose Edit parameters. Change FolderPath to C:\Users\Public\Documents\SalesData\ and click OK, then Apply changes.

    What you should see: the refresh runs and the card shows 1.30M again. You did not open Power Query.

  9. Click File → Export → Power BI template, type a description such as "Sales report. Set FolderPath to the folder with pbi_sales.csv" and save as Lab-10-sales.pbit.

  10. Close Power BI and double-click Lab-10-sales.pbit.

    What you should see: a window asking for FolderPath before anything loads. Enter C:\Users\Public\Documents\SalesData\ and click Load. This is exactly what Rahul would do.

Ravindra Bagale's Tip

Ninety percent of parameter errors are one missing backslash. C:\PowerBI\Labs + pbi_sales.csv becomes C:\PowerBI\Labspbi_sales.csv, and the file is not found. Always end the folder with a backslash. Ek chhota slash, motha farak!

Common mistakes

Mistake What happens Fix
FolderPath without the last backslash "Could not find file …Labspbi_sales.csv" End the value with \
Typing "FolderPath" in quotes Power Query looks for a file literally called FolderPath No quotes around the parameter name
Parameter type left as Any Works, but some dialogs will not offer it Set Type to Text
Copying the file instead of moving it in step 7 Refresh still works and you do not see the error Move (cut) it, so the old path really is empty
Using the parameter in only one of many queries Other queries still break on Rahul's laptop Use FolderPath in the Source step of every file query
Looking for "Edit parameters" inside Power Query It is on the main window Home → Transform data ▼ → Edit parameters

Self-check checklist

0 of 5 done

Try-at-home challenge

Your manager wants the same report for two branches whose files have different names: pbi_sales.csv and pune_sales.csv. Make the file name a second parameter, FileName, so both the folder and the file can change.

Check your answer

Create a second Text parameter FileName with the current value pbi_sales.csv. Change the Source step to:

= Csv.Document(File.Contents(FolderPath & FileName), [Delimiter=",", Columns=7, Encoding=65001, QuoteStyle=QuoteStyle.None])

Now Edit parameters shows two boxes. To test, copy pbi_sales.csv as pune_sales.csv, set FileName to it and refresh: still 24 rows and 1.30M, because the content is the same. With a real Pune file the numbers would change.

Samjla ka? Put the folder in a parameter, join it with &, and end it with a backslash. Aata pudhe jaauya: build a proper star schema with a Date, Product and City table.