<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Power Query on Hat Full of Data</title><link>https://hatfullofdata.blog/categories/power-query/</link><description>Recent content in Power Query on Hat Full of Data</description><generator>Hugo -- gohugo.io</generator><language>en-gb</language><lastBuildDate>Tue, 08 Jul 2025 16:03:02 +0000</lastBuildDate><atom:link href="https://hatfullofdata.blog/categories/power-query/index.xml" rel="self" type="application/rss+xml"/><item><title>Paginated Reports – Power Query as a Source</title><link>https://hatfullofdata.blog/paginated-reports-power-query-as-a-source/</link><pubDate>Tue, 08 Jul 2025 16:01:05 +0000</pubDate><guid>https://hatfullofdata.blog/paginated-reports-power-query-as-a-source/</guid><description>&lt;img src="https://hatfullofdata.blog/paginated-reports-power-query-as-a-source/cover.png" alt="Featured image of post Paginated Reports – Power Query as a Source" /&gt;&lt;p&gt;In March 2024 Microsoft added Power Query as a source for Paginated Reports. This means a paginated report can connect to any data source that Power Query can connect to. This post walks through the process with a few gotchas to be aware of.&lt;/p&gt;
&lt;h2 id="paginated-reports-have-had-some-love"&gt;Paginated Reports have had some Love
&lt;/h2&gt;&lt;p&gt;This post is part of the series covering my session titled Paginated Reports have had some Love. The resources for this series can be found at &lt;a class="link" href="https://github.com/HatFullOfData/Sessions/tree/main/Paginated-Reports-have-had-some-love" target="_blank" rel="noopener"
 &gt;https://github.com/HatFullOfData/Sessions/tree/main/Paginated-Reports-have-had-some-love&lt;/a&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/paginated-reports-power-query-as-a-source/" target="_blank" rel="noopener"
 &gt;Power Query as a Source&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Create a report online&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Dynamic Subscribing&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="get-data"&gt;Get Data
&lt;/h2&gt;&lt;p&gt;As with most reporting the first task is connect to the data using Power Query as a source. Inside a report in Report Builder, click on the data ribbon. Click on Get data to launch the Choose data source dialog. From here you can search and find lots of different data sources. For this post we are going to use an Excel workbook, so click on that.&lt;/p&gt;
&lt;p&gt;When the connection settings dialog opens enter the File path or URL for the Excel file. There is a button to browse OneDrive, bad idea in my opinion but that is a different post! Please note you need to select the correct Authentication type. I’ve used Anonymous as my file is public on GitHub, you will need Organization Account for OneDrive or SharePoint.&lt;/p&gt;
&lt;p&gt;Then click Next.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Get data button and the choose data source dialog and then the connection settings to start using Power Query as a source" class="gallery-image" data-flex-basis="264px" data-flex-grow="110" height="618" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/paginated-reports-power-query-as-a-source/image.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;The next dialog, Choose Data, asks you to select the tables or sheets. Once you have ticked at least one you can then click Transform data to move to the Power Query window.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Choose data dialog step in setting up Power Query as a source" class="gallery-image" data-flex-basis="466px" data-flex-grow="194" height="350" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/paginated-reports-power-query-as-a-source/image-1.png" width="680"&gt;&lt;/p&gt;
&lt;h2 id="power-query-editor"&gt;Power Query Editor
&lt;/h2&gt;&lt;p&gt;When the Power Query window opens you can add steps as per normal such as choosing columns etc. After you add all the transformations you need, click Create to add it to the report.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Power Query window" class="gallery-image" data-flex-basis="344px" data-flex-grow="143" height="474" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/paginated-reports-power-query-as-a-source/image-2.png" width="680"&gt;&lt;/p&gt;
&lt;h2 id="power-query-as-a-source-in-explorer"&gt;Power Query as a source in Explorer
&lt;/h2&gt;&lt;p&gt;When the data loads into the report it comes in 2 parts. Under data sources is Power Query and in datasets are the individual queries. Unlike other data sources such as a database each dataset will be a brand new query.&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="490px" data-flex-grow="204" height="333" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/paginated-reports-power-query-as-a-source/image-3.png" width="680"&gt;&lt;/p&gt;
&lt;h2 id="editing-the-query"&gt;Editing the Query
&lt;/h2&gt;&lt;p&gt;When you need to edit the query, right click on the query and select Edit Mashup. It will always request you configure the connection. No Idea why but clicking the Configure Connection fixes the issue.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Configure connection error on a yellow background" class="gallery-image" data-flex-basis="886px" data-flex-grow="369" height="184" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/paginated-reports-power-query-as-a-source/image-4.png" width="680"&gt;&lt;/p&gt;
&lt;h2 id="a-few-gotchas"&gt;A few Gotchas
&lt;/h2&gt;&lt;h3 id="multiple-queries"&gt;Multiple Queries
&lt;/h3&gt;&lt;p&gt;Each query can only return a single table. So when you have multiple queries, which one does it return? It returns the last one in the list of queries. It has nothing to do with marking a query for loading or not loading.&lt;/p&gt;
&lt;h3 id="refresh-or-not"&gt;Refresh or not?
&lt;/h3&gt;&lt;p&gt;When you publish the report there is no semantic model. There is just a report. So every time the report is opened the Power Query executes. So lets keep those queries tidy and efficient.&lt;/p&gt;
&lt;h3 id="parameters"&gt;Parameters
&lt;/h3&gt;&lt;p&gt;Parameters make Paginated Reports reusable and a powerful tool for the analyst. Power Query parameters though do not connect to the parameters in Report Builder. The Dataset properties show the M code, kind of. Its not editable so we can’t embed the Report Builder parameters. The only way to perform a filter is using the Filters in dataset properties which has the disadvantage that it filters after pulling all the data in.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Dataset properties" class="gallery-image" data-flex-basis="278px" data-flex-grow="116" height="586" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/paginated-reports-power-query-as-a-source/image-6.png" width="680"&gt;&lt;/p&gt;
&lt;h2 id="power-query-as-a-source-conclusion"&gt;Power Query as a Source Conclusion
&lt;/h2&gt;&lt;p&gt;I love Power Query, second to SQL, every data handler in the Microsoft stack should know it. But I don’t believe it brings any benefit putting it in report builder when I can do more from a semantic model. The gotchas aren’t a real problem on their own but they add up. So my recommendation would be to build the semantic model in Power BI desktop or in Microsoft Fabric and connect to that. That is a separate blog post, link up above.&lt;/p&gt;</description></item><item><title>Power Query – Handling Null Values Properly</title><link>https://hatfullofdata.blog/power-query-handling-null-values/</link><pubDate>Mon, 13 Jan 2025 10:53:43 +0000</pubDate><guid>https://hatfullofdata.blog/power-query-handling-null-values/</guid><description>&lt;p&gt;This post is about handling null values in Power Query calculations. If you do a calculation in Power Query that involves a null value the answer returned is null. In the example below we get nulls for the first three rows because either Credit or Debit values are null. On the fourth row we get the answer 460 because Credit and Debit contain values.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Table of data with Date, Description, Credit and Debit columns with an extra calculated column Total and the code is below and for the first three rows shows nulls." class="gallery-image" data-flex-basis="1108px" data-flex-grow="461" height="147" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-handling-null-values/2024-12-31_12-22-52.png" width="679"&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-xml" data-lang="xml"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;= Table.AddColumn(#&amp;#34;Changed Type&amp;#34;, &amp;#34;Total&amp;#34;, each [Credit] - [Debit] )
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;This post gives you 2 methods on how handle them. I personally prefer method 2, but each to their own.&lt;/p&gt;
&lt;h2 id="method-1--replace-null-values"&gt;Method 1 – Replace Null Values
&lt;/h2&gt;&lt;p&gt;The most common method recommended is to replace null values with a value that works, so in the above example a 0. So before I add the above calculation I highlight both the Credit and Debit columns. Then I click replace values on the Transform ribbon.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Replace values dialog with null in the Value to find and 0 in the Replace With box. And the final result showing no nulls." class="gallery-image" data-flex-basis="357px" data-flex-grow="148" height="457" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-handling-null-values/2024-12-31_12-28-19.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;The above works and is simple to explain and document. It does have the side effect that the average Credit and average Debit are now altered and filters to find just credits or debits will have to be different. It also takes 2 steps. The averages issue is the one that most people dislike.&lt;/p&gt;
&lt;h2 id="method-2--using-the-coalesce-operator-with-null-values"&gt;Method 2 – Using the Coalesce operator with Null Values
&lt;/h2&gt;&lt;p&gt;Coalesce functions, DAX has one, return the first non-null value. In Power Query there is a coalesce operator ??. So Credit ?? 0 will return the credit value unless it is null and then will return 0.&lt;/p&gt;
&lt;p&gt;So we can change our calculation to put ?? 0 after the column values and make it not return nulls. The Credit and Debit columns still have their original values for averages etc. It also is done within one step.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Table of data with Date, Description, Credit and Debit columns with an extra calculated column Total using the new calculation shown below and there are no nulle nulls." class="gallery-image" data-flex-basis="1117px" data-flex-grow="465" height="146" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-handling-null-values/2024-12-31_12-45-11.png" width="680"&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-xml" data-lang="xml"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;= Table.AddColumn(#&amp;#34;Changed Type&amp;#34;, &amp;#34;Total&amp;#34;, each [Credit] ?? 0 - [Debit] ?? 0 )
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h2 id="conclusion"&gt;Conclusion
&lt;/h2&gt;&lt;p&gt;Power Query M is one of my favourite languages but its weird. So these posts are for purely selfish reasons to remind me how to do things. ?? is a really simple operator that solves so many problems and Microsoft give it 2 lines! &lt;a class="link" href="https://learn.microsoft.com/en-us/powerquery-m/m-spec-operators" target="_blank" rel="noopener"
 &gt;https://learn.microsoft.com/en-us/powerquery-m/m-spec-operators&lt;/a&gt;. It could of course be chained to Column1 ?? Column2 ?? Column3 will give the the first non-null value from the three columns, and return null if they are all null.&lt;/p&gt;
&lt;h2 id="more-power-query-posts"&gt;More Power Query Posts
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-handwritten-function/" target="_blank" rel="noopener"
 &gt;Custom Handwritten Function&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-multi-step-function/" target="_blank" rel="noopener"
 &gt;Multi-step Function&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-replace-values-for-whole-table/" target="_blank" rel="noopener"
 &gt;Replace Values for Whole Table&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-ai-insights-error/" target="_blank" rel="noopener"
 &gt;AI Insights Error&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/excel-power-query-vba-to-edit-a-parameter-value/" target="_blank" rel="noopener"
 &gt;VBA to Edit a Parameter Value&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-dynamic-data-source-and-web-content/" target="_blank" rel="noopener"
 &gt;Dynamic Data Source and Web.Contents()&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-get-previous-row-data/" target="_blank" rel="noopener"
 &gt;Get Previous Row Data&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-creating-new-parameters/" target="_blank" rel="noopener"
 &gt;Creating New Parameters&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-fixing-missing-columns-dynamically/" target="_blank" rel="noopener"
 &gt;Fixing Missing Columns Dynamically&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-handling-null-values/" target="_blank" rel="noopener"
 &gt;Handling Null Values Properly&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Power Query – Fixing Missing Columns Dynamically</title><link>https://hatfullofdata.blog/power-query-fixing-missing-columns-dynamically/</link><pubDate>Wed, 08 Jan 2025 11:49:26 +0000</pubDate><guid>https://hatfullofdata.blog/power-query-fixing-missing-columns-dynamically/</guid><description>&lt;img src="https://hatfullofdata.blog/power-query-fixing-missing-columns-dynamically/cover.png" alt="Featured image of post Power Query – Fixing Missing Columns Dynamically" /&gt;&lt;p&gt;One of the challenges of making sure your query works in Power Query is data sources that have a changing schema. Some APIs miss out fields if the data is null and sometimes we need one report able to handle data files that are almost the same but not quite. In this post we will look at fixing 2 scenarios, a single missing column and multiple missing columns.&lt;/p&gt;
&lt;p&gt;You know you have this problem when you get the an error similar to this:&lt;/p&gt;
&lt;p&gt;&lt;img alt="An error stating Expression Error, the column Debit of the table wasn’t found" class="gallery-image" data-flex-basis="1255px" data-flex-grow="523" height="130" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-fixing-missing-columns-dynamically/2025-01-08_10-09-49.png" width="680"&gt;&lt;/p&gt;
&lt;h2 id="single-missing-column"&gt;Single Missing Column
&lt;/h2&gt;&lt;p&gt;This scenario is fixing one column missing. In my example the query was written based on a file that had a Debit column. The query is now pointed to file that is missing that column. We need the query to work if the Debit column is in the original or not.&lt;/p&gt;
&lt;p&gt;&lt;img alt="2 screen shots of a query, first one with the last applied step selected and an missing columns error showing, second one with the previous step selected and no error showing" class="gallery-image" data-flex-basis="464px" data-flex-grow="193" height="351" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-fixing-missing-columns-dynamically/2025-01-08_10-14-49.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;Start in the broken version and find the first step where there is not an error. The fix is to add the Debit column if the table does not have the column.&lt;/p&gt;
&lt;p&gt;M is not friendly to write so lets get the interface to write part of it. Click on Custom Column on the Add Column ribbon. Click Insert when prompted about inserting a step. Then enter in the name of the missing column for the column name and null for the value. When you click OK the column gets inserted and your query goes back to working.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Custom column dialog box showing the Debit column with a null value and the result table with the Debit column added." class="gallery-image" data-flex-basis="276px" data-flex-grow="115" height="590" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-fixing-missing-columns-dynamically/2025-01-08_10-27-53.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;But the query will fail when the file does include the Debit column. So we need to add some logic to the new step we added. We need to test to see if the table already has the column, if it does just return the table, if not add the column. This can be done using Table.HasColumns function and an “if” statement.&lt;/p&gt;
&lt;p&gt;The line of M created by the inserting a custom column is&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-xml" data-lang="xml"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;= Table.AddColumn(#&amp;#34;Promoted Headers&amp;#34;, &amp;#34;Debit&amp;#34;, each null)
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;This needs to become&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-xml" data-lang="xml"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;= if Table.HasColumns(#&amp;#34;Promoted Headers&amp;#34;,&amp;#34;Debit&amp;#34;) 
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; then #&amp;#34;Promoted Headers&amp;#34; 
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; else Table.AddColumn(#&amp;#34;Promoted Headers&amp;#34;, &amp;#34;Debit&amp;#34;, each null)
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;Please note the #”Promoted Headers” comes from the name of the previous step.&lt;/p&gt;
&lt;p&gt;Reference for HasColumns can be found at &lt;a class="link" href="https://learn.microsoft.com/en-us/powerquery-m/table-hascolumns?wt.mc_id=DX-MVP-5003563" target="_blank" rel="noopener"
 &gt;https://learn.microsoft.com/en-us/powerquery-m/table-hascolumns&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="multiple-missing-columns"&gt;Multiple Missing Columns
&lt;/h2&gt;&lt;p&gt;The previous method works great for a single column and maybe even 2 but if you have more columns than that you really need another method. In this example we have a set of data with columns for months Jan – Dec. At the start of the year there is only a Jan column but we need the query to return all the columns.&lt;/p&gt;
&lt;p&gt;&lt;img alt="error complaining about missing columns" class="gallery-image" data-flex-basis="1133px" data-flex-grow="472" height="144" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-fixing-missing-columns-dynamically/2025-01-08_10-54-24.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;The error will only complain about the first column it finds missing but if we fixed Feb it would then complain that Mar was missing etc. When you append 2 tables it creates a table with all the columns of both tables even if one table does not contain any data. So if we can create an empty table with all the columns we want and then append the tables we will get all the columns added.&lt;/p&gt;
&lt;p&gt;Create a new blank query and enter in the formula to create a list of all the column names you want to make sure exist. Use {} brackets containing the column name strings separated by commas. This will display the list of values. Rename the query to something meaningful, for example Months.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-xml" data-lang="xml"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;= {&amp;#34;Jan&amp;#34;,&amp;#34;Feb&amp;#34;,&amp;#34;Mar&amp;#34;,&amp;#34;Apr&amp;#34;,&amp;#34;May&amp;#34;,&amp;#34;Jun&amp;#34;,&amp;#34;Jul&amp;#34;,&amp;#34;Aug&amp;#34;,&amp;#34;Sep&amp;#34;,&amp;#34;Oct&amp;#34;,&amp;#34;Nov&amp;#34;,&amp;#34;Dec&amp;#34;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;&lt;img alt="Screen shot from Power Query showing the query with formula shown as the Source step." class="gallery-image" data-flex-basis="932px" data-flex-grow="388" height="175" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-fixing-missing-columns-dynamically/2025-01-08_11-01-40.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;Now we need to add a step that converts this into a table. Right click on the Source step and select Insert Step After. Next we use #table function that takes 2 parameters, list of columns and values. We want an empty table so will use {} for the values. Change the formula to the following&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-xml" data-lang="xml"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;= #table( Source , {} )
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="1554px" data-flex-grow="647" height="105" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-fixing-missing-columns-dynamically/2025-01-08_11-07-21.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;Reference for #table can be found at &lt;a class="link" href="https://learn.microsoft.com/en-us/powerquery-m/sharptable?wt.mc_id=DX-MVP-5003563" target="_blank" rel="noopener"
 &gt;https://learn.microsoft.com/en-us/powerquery-m/sharptable&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Return to the broken query and find the last step that the query works. On the Home ribbon select Append Queries and select Insert when prompted about inserting a step. In the Append dialog select the query that has the empty table just created. Then click OK. Your query now has all the columns required and should work if the columns are missing columns or not&lt;/p&gt;
&lt;p&gt;&lt;img alt="Append dialog showing appending the Months table and the final table with the missing columns added" class="gallery-image" data-flex-basis="381px" data-flex-grow="158" height="428" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-fixing-missing-columns-dynamically/2025-01-08_11-08-49.png" width="680"&gt;&lt;/p&gt;
&lt;h2 id="advanced-notes-on-missing-columns"&gt;Advanced Notes on Missing Columns
&lt;/h2&gt;&lt;p&gt;When appending 2 tables the first table columns will come first and in the order of the first table. So if you wish to order the columns of the query as well make sure your blank table query contains all the columns and the swap the order of the tables named in the Table.Combine step.&lt;/p&gt;
&lt;p&gt;The list of column names and creating the blank table could be put as steps within the original query. That requires being comfortable with the Advanced editor and writing M. For those interested here is an example query.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt; 1
&lt;/span&gt;&lt;span class="lnt"&gt; 2
&lt;/span&gt;&lt;span class="lnt"&gt; 3
&lt;/span&gt;&lt;span class="lnt"&gt; 4
&lt;/span&gt;&lt;span class="lnt"&gt; 5
&lt;/span&gt;&lt;span class="lnt"&gt; 6
&lt;/span&gt;&lt;span class="lnt"&gt; 7
&lt;/span&gt;&lt;span class="lnt"&gt; 8
&lt;/span&gt;&lt;span class="lnt"&gt; 9
&lt;/span&gt;&lt;span class="lnt"&gt;10
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-xml" data-lang="xml"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;let
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Source = Excel.Workbook(Web.Contents(ExcelPath), null, true),
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Sheet2_Sheet = Source{[Item=&amp;#34;Sheet2&amp;#34;,Kind=&amp;#34;Sheet&amp;#34;]}[Data],
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; #&amp;#34;Promoted Headers&amp;#34; = Table.PromoteHeaders(Sheet2_Sheet, [PromoteAllScalars=true]),
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; AllColumns = {&amp;#34;Project&amp;#34;,&amp;#34;Jan&amp;#34;,&amp;#34;Feb&amp;#34;,&amp;#34;Mar&amp;#34;,&amp;#34;Apr&amp;#34;,&amp;#34;May&amp;#34;,&amp;#34;Jun&amp;#34;,&amp;#34;Jul&amp;#34;,&amp;#34;Aug&amp;#34;,&amp;#34;Sep&amp;#34;,&amp;#34;Oct&amp;#34;,&amp;#34;Nov&amp;#34;,&amp;#34;Dec&amp;#34;},
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; BlankTable = #table( AllColumns , {} ),
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; #&amp;#34;Appended Query&amp;#34; = Table.Combine({BlankTable, #&amp;#34;Promoted Headers&amp;#34;}),
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; #&amp;#34;Changed Type&amp;#34; = Table.TransformColumnTypes(#&amp;#34;Appended Query&amp;#34;,{{&amp;#34;Project&amp;#34;, type text}, {&amp;#34;Jan&amp;#34;, Int64.Type}, {&amp;#34;Feb&amp;#34;, Int64.Type}, {&amp;#34;Mar&amp;#34;, Int64.Type}, {&amp;#34;Apr&amp;#34;, Int64.Type}, {&amp;#34;May&amp;#34;, Int64.Type}, {&amp;#34;Jun&amp;#34;, Int64.Type}, {&amp;#34;Jul&amp;#34;, Int64.Type}, {&amp;#34;Aug&amp;#34;, Int64.Type}, {&amp;#34;Sep&amp;#34;, Int64.Type}, {&amp;#34;Oct&amp;#34;, Int64.Type}, {&amp;#34;Nov&amp;#34;, Int64.Type}, {&amp;#34;Dec&amp;#34;, Int64.Type}})
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;in
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; #&amp;#34;Changed Type&amp;#34;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h2 id="conclusion"&gt;Conclusion
&lt;/h2&gt;&lt;p&gt;Queries should be dynamic when required and missing columns is a common issue. Writing M is not friendly, writing dynamic M takes practice but it is worth it. Power Query that copes with your unclean data is the reality of handling data. Bast of luck I hope this helped.&lt;/p&gt;
&lt;h2 id="more-power-query-posts"&gt;More Power Query Posts
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-handwritten-function/" target="_blank" rel="noopener"
 &gt;Custom Handwritten Function&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-multi-step-function/" target="_blank" rel="noopener"
 &gt;Multi-step Function&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-replace-values-for-whole-table/" target="_blank" rel="noopener"
 &gt;Replace Values for Whole Table&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-ai-insights-error/" target="_blank" rel="noopener"
 &gt;AI Insights Error&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/excel-power-query-vba-to-edit-a-parameter-value/" target="_blank" rel="noopener"
 &gt;VBA to Edit a Parameter Value&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-dynamic-data-source-and-web-content/" target="_blank" rel="noopener"
 &gt;Dynamic Data Source and Web.Contents()&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-get-previous-row-data/" target="_blank" rel="noopener"
 &gt;Get Previous Row Data&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-creating-new-parameters/" target="_blank" rel="noopener"
 &gt;Creating New Parameters&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-fixing-missing-columns-dynamically/" target="_blank" rel="noopener"
 &gt;Fixing Missing Columns Dynamically&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-handling-null-values/" target="_blank" rel="noopener"
 &gt;Handling Null Values Properly&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Power Query – Creating New Parameters</title><link>https://hatfullofdata.blog/power-query-creating-new-parameters/</link><pubDate>Mon, 06 Jan 2025 11:52:37 +0000</pubDate><guid>https://hatfullofdata.blog/power-query-creating-new-parameters/</guid><description>&lt;img src="https://hatfullofdata.blog/power-query-creating-new-parameters/cover.png" alt="Featured image of post Power Query – Creating New Parameters" /&gt;&lt;p&gt;When fetching data into Power Query you need to use values to point you to the write the data, for example name of a database server or path to a csv file. Good practice is to use parameters for these values so your query is reusable etc. And its always our intention to go and create the new parameter later.&lt;/p&gt;
&lt;h2 id="missing-new-parameter-option"&gt;Missing New Parameter Option
&lt;/h2&gt;&lt;p&gt;So lets take the simplest option of connecting to a csv file on the web. I have my path so I click get data and select Web. For those that want to play along here is the path to the file I’m using.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-xml" data-lang="xml"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;https://raw.githubusercontent.com/HatFullOfData/Demo/refs/heads/main/Products.csv
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;The dialog that appears has a box for the URL but is missing the drop down on the left that will allow me to select New Parameter. If I had already created a parameter it gives me the option but not if I have no parameters. Bizarre functionality.&lt;/p&gt;
&lt;p&gt;&lt;img alt="From Web dialog with a text box for the URL but no dropdown to select New Parameter" class="gallery-image" data-flex-basis="745px" data-flex-grow="310" height="219" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-creating-new-parameters/2024-12-31_17-58-42.png" width="680"&gt;&lt;/p&gt;
&lt;h2 id="always-allow-parameters"&gt;Always Allow Parameters
&lt;/h2&gt;&lt;p&gt;The fix for this is to allow parameterization in data source dialogs. This can be done in the Power BI desktop options or on Power Query View ribbon for Power BI and Excel.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Power BI options dialog with Power Query Editor selected and Parameters highlighted. And the View ribbon in Power Query with the Parameters group highlighted" class="gallery-image" data-flex-basis="370px" data-flex-grow="154" height="440" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-creating-new-parameters/2024-12-31_18-09-27.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;Once this is ticked when you now go to Get Data we get a drop down next to the text box. Now we can select New Parameter and enter in the details of the new parameter. When we click OK it returns to the first dialog with the new parameter selected.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Showing how to create a new parameter by clicking the drop down" class="gallery-image" data-flex-basis="632px" data-flex-grow="263" height="258" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-creating-new-parameters/2024-12-31_18-16-00.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;As a side note I recommend that you always give parameters a Type rather than leaving it on Any. There are a few features such as Power BI deployment pipelines that will not work with parameters of type Any.&lt;/p&gt;
&lt;h2 id="conclusion"&gt;Conclusion
&lt;/h2&gt;&lt;p&gt;This is a really small feature, possibly doesn’t need a whole blog post. But that little drop down making the creating a new parameter that few clicks easier means I am more likely to follow my own advice and use parameters from the start.&lt;/p&gt;</description></item><item><title>Power Query – Get Previous Row Data</title><link>https://hatfullofdata.blog/power-query-get-previous-row-data/</link><pubDate>Tue, 08 Jun 2021 08:55:16 +0000</pubDate><guid>https://hatfullofdata.blog/power-query-get-previous-row-data/</guid><description>&lt;img src="https://hatfullofdata.blog/power-query-get-previous-row-data/cover.png" alt="Featured image of post Power Query – Get Previous Row Data" /&gt;&lt;p&gt;Occasionally I get asked a question via Twitter or LinkedIn that prompts me to write a post. The question asked was how to get previous row data. In this post I will describe how to get a value from the previous row that matches a criteria. The example data is peoples scores on different dates. I want to know if they are improving. For this I need their previous score.&lt;/p&gt;
&lt;h3 id="starting-data"&gt;Starting Data
&lt;/h3&gt;&lt;p&gt;&lt;img alt="Table of data with columns name, date and score" class="gallery-image" data-flex-basis="545px" data-flex-grow="227" height="299" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-get-previous-row-data/PreviousScore_001.png" width="680"&gt;&lt;/p&gt;
&lt;h3 id="getting-previous-row-data"&gt;Getting Previous Row Data
&lt;/h3&gt;&lt;p&gt;The first step is to sort the data. You need the previous score to be in the previous row, so we sort by name and then date.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Data now sorted by Name and Date." class="gallery-image" data-flex-basis="544px" data-flex-grow="226" height="300" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-get-previous-row-data/PreviousScore_002.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;Then from the Add Column ribbon we add an Index column. And then a custom column, “Prev Index” which is Index-1&lt;/p&gt;
&lt;p&gt;&lt;img alt="Custom Column dialog showing the calculation for Prev Index of Index -1" class="gallery-image" data-flex-basis="374px" data-flex-grow="155" height="436" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-get-previous-row-data/PreviousScore_003.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;Now we can merge a table to itself (yes a touch head screwy but its fine it works) by linking Prev Index to Index. This though will give Bob Alice’s last score as his previous score so we add the Name column to the merge as well. Holding down the Ctrl key will allow you to select multiple columns and make sure at the bottom of the dialog you have some matching rows..&lt;/p&gt;
&lt;p&gt;&lt;img alt="Merge dialog with columns selected for the merge" class="gallery-image" data-flex-basis="267px" data-flex-grow="111" height="611" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-get-previous-row-data/PreviousScore_004.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;This creates a column that contains tables. Press the expand button and only select the Score column. Click OK to give a column of the previous scores ready to rename.&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="558px" data-flex-grow="232" height="292" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-get-previous-row-data/PreviousScore_005.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;I then rename Score.1 to Previous Score and use Choose Columns to remove Index and Previous Index to end up with a table of name, date, score and previous score.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Final table including previous row data" class="gallery-image" data-flex-basis="709px" data-flex-grow="295" height="230" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-get-previous-row-data/PreviousScore_006.png" width="680"&gt;&lt;/p&gt;
&lt;h2 id="more-power-query-posts"&gt;More Power Query Posts
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-handwritten-function/" target="_blank" rel="noopener"
 &gt;Custom Handwritten Function&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-multi-step-function/" target="_blank" rel="noopener"
 &gt;Multi-step Function&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-replace-values-for-whole-table/" target="_blank" rel="noopener"
 &gt;Replace Values for Whole Table&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-ai-insights-error/" target="_blank" rel="noopener"
 &gt;AI Insights Error&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/excel-power-query-vba-to-edit-a-parameter-value/" target="_blank" rel="noopener"
 &gt;VBA to Edit a Parameter Value&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-dynamic-data-source-and-web-content/" target="_blank" rel="noopener"
 &gt;Dynamic Data Source and Web.Contents()&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-get-previous-row-data/" target="_blank" rel="noopener"
 &gt;Get Previous Row Data&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-creating-new-parameters/" target="_blank" rel="noopener"
 &gt;Creating New Parameters&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-fixing-missing-columns-dynamically/" target="_blank" rel="noopener"
 &gt;Fixing Missing Columns Dynamically&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-handling-null-values/" target="_blank" rel="noopener"
 &gt;Handling Null Values Properly&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Power Query – Dynamic Data Source and Web.Contents()</title><link>https://hatfullofdata.blog/power-query-dynamic-data-source-and-web-content/</link><pubDate>Tue, 04 May 2021 13:01:31 +0000</pubDate><guid>https://hatfullofdata.blog/power-query-dynamic-data-source-and-web-content/</guid><description>&lt;img src="https://hatfullofdata.blog/power-query-dynamic-data-source-and-web-content/cover.png" alt="Featured image of post Power Query – Dynamic Data Source and Web.Contents()" /&gt;&lt;p&gt;This is the first post in a series to create a Star Wars report. This post handles the issue of using a parameter as the url in a web Power Query. This type of data source is referred to as a Dynamic Data Source. It will not refresh in the Power BI service unless you do some tricks.&lt;/p&gt;
&lt;h3 id="star-wars-series"&gt;Star Wars Series
&lt;/h3&gt;&lt;p&gt;Starting on May 4th a series using Star Wars data had to be done. Its based off an api that has some great data that is paged in 10 row blocks. I’m looking to create complete report from queries to modelling to visuals and theme etc.&lt;/p&gt;
&lt;p&gt;The data comes from &lt;a class="link" href="https://swapi.dev/api/" target="_blank" rel="noopener"
 &gt;https://swapi.dev/api/&lt;/a&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-dynamic-data-source-and-web-contents/" target="_blank" rel="noopener"
 &gt;Handling Dynamic Data sources&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Fetching number of pages&lt;/li&gt;
&lt;li&gt;Using next page url&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="fetching-data"&gt;Fetching Data
&lt;/h3&gt;&lt;p&gt;I started by looking at the people table. In a new Power BI report I select Web from Get Data and use the url &lt;a class="link" href="https://swapi.dev/api/people/" target="_blank" rel="noopener"
 &gt;https://swapi.dev/api/people/&lt;/a&gt; and click OK. This throws you straight into Power Query where is has created a few steps to get you a table of data.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Table of 10 rows of data plus 5 query steps in Power Query" class="gallery-image" data-flex-basis="796px" data-flex-grow="331" height="205" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-dynamic-data-source-and-web-content/API-Paged-001.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;It only gives 10 rows of data, which does include the url to the next 10 rows and also how many rows there are in total. So I use a technique I’ve blogged about before and create a URL parameter, change the source step to use the URL and then convert the query into a function.&lt;/p&gt;
&lt;p&gt;Look here for more details &lt;a class="link" href="https://hatfullofdata.blog/power-query-fetch-web-data/" target="_blank" rel="noopener"
 &gt;https://hatfullofdata.blog/power-query-fetch-web-data/&lt;/a&gt;&lt;/p&gt;
&lt;h3 id="test-refresh"&gt;Test Refresh
&lt;/h3&gt;&lt;p&gt;Then as a test I invoke the function to fetch the first 10 rows into a table, close and apply Power Query and then publish my report to the Power BI service.&lt;/p&gt;
&lt;p&gt;&lt;img alt="snapshot of the power bi workspace showing the report and dataset listed" class="gallery-image" data-flex-basis="466px" data-flex-grow="194" height="350" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-dynamic-data-source-and-web-content/API-Paged-002.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;Then I do the important test, does it refresh? When I click on refresh it fails, and when I click on the small red triangle it gives an error stating the dataset uses a dynamic data source and these can’t be refreshed.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Screen grab of the Dynamic Data Source refresh error." class="gallery-image" data-flex-basis="570px" data-flex-grow="237" height="286" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-dynamic-data-source-and-web-content/API-Paged-003.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;Part of the error includes a URL &lt;a class="link" href="https://aka.ms/dynamic-data-sources" target="_blank" rel="noopener"
 &gt;https://aka.ms/dynamic-data-sources&lt;/a&gt; , so I go and look at the web page. And it explains how you can identify if you have a dynamic data source that won’t refresh by looking in the Data source settings in Power Query. Sure enough, when I look, it gives a message which I now know means a dynamic data source so refresh won’t work. I assume hand-authored queries means dynamic data sources.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Data source settings dialog with message regarding hand-crafted queries which is how you identify Dynamic Data Sources exist apparently" class="gallery-image" data-flex-basis="418px" data-flex-grow="174" height="390" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-dynamic-data-source-and-web-content/API-Paged-004.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;This doesn’t tell me how to to fix it though. So off onto the internet I go and search around my favourite Power Query bloggers and sure enough Chris Webb comes up trumps with a few posts.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://blog.crossjoin.co.uk/2016/08/23/web-contents-m-functions-and-dataset-refresh-errors-in-power-bi/" target="_blank" rel="noopener"
 &gt;Chris Webb’s BI Blog: Web.Contents(), M Functions And Dataset Refresh Errors In Power BI Chris Webb’s BI Blog (crossjoin.co.uk)&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://blog.crossjoin.co.uk/2016/08/16/using-the-relativepath-and-query-options-with-web-contents-in-power-query-and-power-bi-m-code/" target="_blank" rel="noopener"
 &gt;Chris Webb’s BI Blog: Using The RelativePath And Query Options With Web.Contents() In Power Query And Power BI M Code Chris Webb’s BI Blog (crossjoin.co.uk)&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The second post was the one I used the most in modifying my query. It also includes searching for cow data, which I think is superb!&lt;/p&gt;
&lt;h3 id="removing-the-dynamic-data-source"&gt;Removing the Dynamic Data Source
&lt;/h3&gt;&lt;p&gt;So the Web.Contents used in the source of my function needs the base url to stay the same and then any dynamic parts put into the options. The base url must be a valid web address you can authenticate to as anonymous and the shorter the better as it makes the function more flexible. I delete the invoked function query and the function from my report.&lt;/p&gt;
&lt;p&gt;So for the Star Wars data the base url is – &lt;a class="link" href="https://swapi.dev/api" target="_blank" rel="noopener"
 &gt;https://swapi.dev/api&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The people part and the page=1 can be added in as part of the options. So the new solution has 2 parameters, TableName and PageNum and they are both text. Then we have to edit the source step by hand to write the second parameter of Web.Con (the whole hand-crafted fix to get rid of hand-crafted error is just amusing). The options are a record so put inside square brackets [ .. ]. The query is another record so is a nested [ .. ].&lt;/p&gt;
&lt;p&gt;Web.Contents options allow for two things to be set, a relative path, eg People and query parameters eg Page=1. APIs are fussy so make sure you get the case right in the query part. This is my final code for the Source step. (This is the reason I blog, so in 6 months time I know I have a working example piece of code.)&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;span class="lnt"&gt;6
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-xml" data-lang="xml"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Source = Json.Document(Web.Contents(&amp;#34;https://swapi.dev/api/&amp;#34;,
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; [
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; RelativePath=TableName,
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Query=[page=PageNum]
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; ]
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; )),
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;Then I right click on the query and create a function. Invoking this function creates a table ready now to close and apply back into Power BI desktop. I publish and try a refresh and YES! it works. I now have a function that will take a table name and page number and give 10 rows of data.&lt;/p&gt;
&lt;p&gt;In the next post we will use this function to get all the data for a table.&lt;/p&gt;
&lt;h2 id="more-power-query-posts"&gt;More Power Query Posts
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-handwritten-function/" target="_blank" rel="noopener"
 &gt;Custom Handwritten Function&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-multi-step-function/" target="_blank" rel="noopener"
 &gt;Multi-step Function&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-replace-values-for-whole-table/" target="_blank" rel="noopener"
 &gt;Replace Values for Whole Table&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-ai-insights-error/" target="_blank" rel="noopener"
 &gt;AI Insights Error&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/excel-power-query-vba-to-edit-a-parameter-value/" target="_blank" rel="noopener"
 &gt;VBA to Edit a Parameter Value&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-dynamic-data-source-and-web-content/" target="_blank" rel="noopener"
 &gt;Dynamic Data Source and Web.Contents()&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-get-previous-row-data/" target="_blank" rel="noopener"
 &gt;Get Previous Row Data&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-creating-new-parameters/" target="_blank" rel="noopener"
 &gt;Creating New Parameters&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-fixing-missing-columns-dynamically/" target="_blank" rel="noopener"
 &gt;Fixing Missing Columns Dynamically&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-handling-null-values/" target="_blank" rel="noopener"
 &gt;Handling Null Values Properly&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Power Query – VBA to Edit a Parameter Value</title><link>https://hatfullofdata.blog/excel-power-query-vba-to-edit-a-parameter-value/</link><pubDate>Wed, 21 Apr 2021 17:04:16 +0000</pubDate><guid>https://hatfullofdata.blog/excel-power-query-vba-to-edit-a-parameter-value/</guid><description>&lt;img src="https://hatfullofdata.blog/excel-power-query-vba-to-edit-a-parameter-value/cover.png" alt="Featured image of post Power Query – VBA to Edit a Parameter Value" /&gt;&lt;p&gt;This post is to document how to use Excel VBA to edit a parameter value without using the a cell reference in Power Query. My query was using Web.Contents which works with parameters in Excel Power Query1 but doesn’t like a function as part of the path.&lt;/p&gt;
&lt;p&gt;I started with Chris Webb’s great post on using an Excel named range and Web.Contents gave me problems. I would try Chris Webb’s method first which is found here &lt;a class="link" href="https://blog.crossjoin.co.uk/2014/07/22/working-with-excel-named-ranges-in-power-query/" target="_blank" rel="noopener"
 &gt;https://blog.crossjoin.co.uk/2014/07/22/working-with-excel-named-ranges-in-power-query/&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;So I want a button to take the value from a cell and update the value in a parameter. I searched the web and found one solution buried in a MrExcel forum so I’m documenting the solution here mostly so I can find it again later.&lt;/p&gt;
&lt;h3 id="vba-code-to-edit-a-parameter-value"&gt;VBA Code to Edit a Parameter Value
&lt;/h3&gt;&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt; 1
&lt;/span&gt;&lt;span class="lnt"&gt; 2
&lt;/span&gt;&lt;span class="lnt"&gt; 3
&lt;/span&gt;&lt;span class="lnt"&gt; 4
&lt;/span&gt;&lt;span class="lnt"&gt; 5
&lt;/span&gt;&lt;span class="lnt"&gt; 6
&lt;/span&gt;&lt;span class="lnt"&gt; 7
&lt;/span&gt;&lt;span class="lnt"&gt; 8
&lt;/span&gt;&lt;span class="lnt"&gt; 9
&lt;/span&gt;&lt;span class="lnt"&gt;10
&lt;/span&gt;&lt;span class="lnt"&gt;11
&lt;/span&gt;&lt;span class="lnt"&gt;12
&lt;/span&gt;&lt;span class="lnt"&gt;13
&lt;/span&gt;&lt;span class="lnt"&gt;14
&lt;/span&gt;&lt;span class="lnt"&gt;15
&lt;/span&gt;&lt;span class="lnt"&gt;16
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-xml" data-lang="xml"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Sub ChangeParameterValue(ParameterName As String, ParameterValue As String)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Dim qry As WorkbookQuery
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Dim formula As Variant
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &amp;#39;=== Get the query
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Set qry = ThisWorkbook.Queries(ParameterName)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &amp;#39;=== Split the formula into 3 parts and update the second one
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; formula = Split(qry.formula, Chr(34), 3)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; formula(1) = ParameterValue
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &amp;#39;=== Update the parameter value
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; qry.formula = Join(formula, Chr(34))
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;End Sub
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h3 id="description"&gt;Description
&lt;/h3&gt;&lt;p&gt;The formula property of a query is really weird and hard to construct so the above function splits on Chr(34), which is a “. It then updates the middle value to the new value and then sticks the three values back together again.&lt;/p&gt;
&lt;p&gt;The original forum post I got the solution from can be found at &lt;a class="link" href="https://www.mrexcel.com/board/threads/vba-code-to-edit-power-query-data-source-settings.1146964/" target="_blank" rel="noopener"
 &gt;https://www.mrexcel.com/board/threads/vba-code-to-edit-power-query-data-source-settings.1146964/&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Footnotes&lt;/p&gt;
&lt;p&gt;1 – Web.Contents varies in different versions of Power Query so test carefully and do not assume you can copy and paste queries.&lt;/p&gt;
&lt;h2 id="more-power-query-posts"&gt;More Power Query Posts
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-handwritten-function/" target="_blank" rel="noopener"
 &gt;Custom Handwritten Function&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-multi-step-function/" target="_blank" rel="noopener"
 &gt;Multi-step Function&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-replace-values-for-whole-table/" target="_blank" rel="noopener"
 &gt;Replace Values for Whole Table&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-ai-insights-error/" target="_blank" rel="noopener"
 &gt;AI Insights Error&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/excel-power-query-vba-to-edit-a-parameter-value/" target="_blank" rel="noopener"
 &gt;VBA to Edit a Parameter Value&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-dynamic-data-source-and-web-content/" target="_blank" rel="noopener"
 &gt;Dynamic Data Source and Web.Contents()&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-get-previous-row-data/" target="_blank" rel="noopener"
 &gt;Get Previous Row Data&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-creating-new-parameters/" target="_blank" rel="noopener"
 &gt;Creating New Parameters&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-fixing-missing-columns-dynamically/" target="_blank" rel="noopener"
 &gt;Fixing Missing Columns Dynamically&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-handling-null-values/" target="_blank" rel="noopener"
 &gt;Handling Null Values Properly&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Power Query – AI Insights Error</title><link>https://hatfullofdata.blog/power-query-ai-insights-error/</link><pubDate>Fri, 15 Jan 2021 22:09:52 +0000</pubDate><guid>https://hatfullofdata.blog/power-query-ai-insights-error/</guid><description>&lt;img src="https://hatfullofdata.blog/power-query-ai-insights-error/cover.png" alt="Featured image of post Power Query – AI Insights Error" /&gt;&lt;p&gt;AI Insights in Power Query is a great Premium feature in Power BI. An AI Insights Error can occur if you are not fully aware of the settings you need to check. This foxed me for 2+ hours today. So I hope this post helps someone not waste the 2 hours like me.&lt;/p&gt;
&lt;p&gt;From the Add Columns ribbon I selected Text Analytics and then in the Text Analytics I selected Extract key phrases, but this is true for any of the AI functions.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Selecting Text Analytics" class="gallery-image" data-flex-basis="458px" data-flex-grow="191" height="356" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-ai-insights-error/Error_002.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;It thought for a while, and I assumed it was processing. And then it gave this error. I tried every combination of what I could do to get rid of the error.&lt;/p&gt;
&lt;p&gt;&lt;img alt="AI Insights Error in Power Query that states\nWe cannot convert the value Function to type Function." class="gallery-image" data-flex-basis="1157px" data-flex-grow="482" height="141" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-ai-insights-error/Error_001.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;The issue is when you do an AI function it creates a data source with a Public privacy level. So if your data that you are analysing has a different privacy level you get the error above.&lt;/p&gt;
&lt;h3 id="fixing-privacy-levels"&gt;Fixing Privacy Levels
&lt;/h3&gt;&lt;p&gt;This issue has a really quick fix once you realise what it is caused by. On the home ribbon in Power Query, click on Data source setting. This opens the Data Source setting dialog box.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Data source setting dialog" class="gallery-image" data-flex-basis="435px" data-flex-grow="181" height="375" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-ai-insights-error/Error_003.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;Click on each data source and click on Edit Permissions. Change the privacy level to be the same, I use Organizational usually.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Edit Permissions dialog" class="gallery-image" data-flex-basis="299px" data-flex-grow="124" height="545" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-ai-insights-error/Error_004.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;When you have checked all your data sources you can click close in the Data Source Settings dialog and refresh your data to get rid of the error.&lt;/p&gt;
&lt;h3 id="conclusion-on-ai-insights-errors"&gt;Conclusion on AI Insights Errors
&lt;/h3&gt;&lt;p&gt;This is a classic case of when you know the cause it’s easy to fix. I hope it helped.&lt;/p&gt;
&lt;h2 id="more-power-query-posts"&gt;More Power Query Posts
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-handwritten-function/" target="_blank" rel="noopener"
 &gt;Custom Handwritten Function&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-multi-step-function/" target="_blank" rel="noopener"
 &gt;Multi-step Function&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-replace-values-for-whole-table/" target="_blank" rel="noopener"
 &gt;Replace Values for Whole Table&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-ai-insights-error/" target="_blank" rel="noopener"
 &gt;AI Insights Error&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/excel-power-query-vba-to-edit-a-parameter-value/" target="_blank" rel="noopener"
 &gt;VBA to Edit a Parameter Value&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-dynamic-data-source-and-web-content/" target="_blank" rel="noopener"
 &gt;Dynamic Data Source and Web.Contents()&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-get-previous-row-data/" target="_blank" rel="noopener"
 &gt;Get Previous Row Data&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-creating-new-parameters/" target="_blank" rel="noopener"
 &gt;Creating New Parameters&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-fixing-missing-columns-dynamically/" target="_blank" rel="noopener"
 &gt;Fixing Missing Columns Dynamically&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-handling-null-values/" target="_blank" rel="noopener"
 &gt;Handling Null Values Properly&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Power BI – London Reactor – Tea &amp; Biscuits Session</title><link>https://hatfullofdata.blog/power-bi-tea-biscuits-session-reactor/</link><pubDate>Sun, 15 Nov 2020 23:07:36 +0000</pubDate><guid>https://hatfullofdata.blog/power-bi-tea-biscuits-session-reactor/</guid><description>&lt;img src="https://hatfullofdata.blog/power-bi-tea-biscuits-session-reactor/cover.png" alt="Featured image of post Power BI – London Reactor – Tea &amp; Biscuits Session" /&gt;&lt;p&gt;This post is to support my virtual introduction to Power BI session for London Reactor on 16th November 2020.&lt;/p&gt;
&lt;h3 id="resources"&gt;Resources
&lt;/h3&gt;&lt;p&gt;Tea Production – &lt;a class="link" href="https://teahow.com/where-is-tea-grown/" target="_blank" rel="noopener"
 &gt;https://teahow.com/where-is-tea-grown/&lt;/a&gt;Favourite Biscuit – &lt;a class="link" href="https://hatfullofdata.blog/wp-content/uploads/2020/11/Biscuit-Data.xlsx" target="_blank" rel="noopener"
 &gt;https://hatfullofdata.blog/wp-content/uploads/2020/11/Biscuit-Data.xlsx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The biscuit data is fictitious using randomly generated names and random selection of biscuits.&lt;/p&gt;
&lt;h3 id="session-recording"&gt;Session Recording
&lt;/h3&gt;&lt;p&gt;&lt;a class="link" href="https://youtu.be/nQESavfh5qI" target="_blank" rel="noopener"
 &gt;&lt;img alt="Link to video" class="gallery-image" data-flex-basis="462px" data-flex-grow="192" height="353" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-bi-tea-biscuits-session-reactor/Tea-and-Biscuits.png" width="680"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="london-reactor-posts"&gt;London Reactor Posts
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/reactor-power-bi-introduction-resources/" target="_blank" rel="noopener"
 &gt;Power BI Introduction&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-bi-introduction-weather-report-resources/" target="_blank" rel="noopener"
 &gt;Weather Report Resources&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-bi-analyse-titanic-data-with-ai-visuals/" target="_blank" rel="noopener"
 &gt;Use AI Visuals to Analyse Titanic Data&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/reactor-christmas-carol-text-analysis/" target="_blank" rel="noopener"
 &gt;Christmas Carol Text Analysis&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-bi-reactor-last-years-sales/" target="_blank" rel="noopener"
 &gt;Last Year’s Sales&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-bi-tea-biscuits-session-reactor/" target="_blank" rel="noopener"
 &gt;Tea &amp;amp; Biscuits&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Power BI – Import Text Using Examples</title><link>https://hatfullofdata.blog/power-bi-import-text-using-examples/</link><pubDate>Sat, 22 Aug 2020 22:22:45 +0000</pubDate><guid>https://hatfullofdata.blog/power-bi-import-text-using-examples/</guid><description>&lt;img src="https://hatfullofdata.blog/power-bi-import-text-using-examples/cover.png" alt="Featured image of post Power BI – Import Text Using Examples" /&gt;&lt;p&gt;Import text using examples was introduced in the 2020 August update for Power BI. It writes the Power Query steps to transform a non-tabular text file into a table.&lt;/p&gt;
&lt;p&gt;&lt;img alt="from import to table" class="gallery-image" data-flex-basis="680px" data-flex-grow="283" height="240" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-bi-import-text-using-examples/TextImport_000.png" width="680"&gt;&lt;/p&gt;
&lt;h3 id="youtube-version"&gt;YouTube Version
&lt;/h3&gt;&lt;p&gt;&lt;img alt="youtube video" class="gallery-image" data-flex-basis="427px" data-flex-grow="178" height="382" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-bi-import-text-using-examples/TextImport_000yt.png" width="680"&gt;&lt;a class="link" href="https://youtu.be/6h7g-CwkeVg" target="_blank" rel="noopener"
 &gt;https://youtu.be/6h7g-CwkeVg&lt;/a&gt;&lt;/p&gt;
&lt;h3 id="enabling-the-feature"&gt;Enabling the Feature
&lt;/h3&gt;&lt;p&gt;At the point of writing this blog post this feature is still in preview. The feature is enabled in File – Options in Preview features. It will like most features move to being enabled for everyone at some point.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Enable import text using examples feature" class="gallery-image" data-flex-basis="356px" data-flex-grow="148" height="458" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-bi-import-text-using-examples/TextImport_001-1.png" width="680"&gt;&lt;/p&gt;
&lt;h3 id="example-import-text-using-examples"&gt;Example Import text using Examples
&lt;/h3&gt;&lt;p&gt;For this example I have a very simple text file as shown in first picture in this post. It contains the details of journeys on different days. So when I select Get Data – Text/CSV and select the file, the dialog that shows a preview of the data in the file has a new button.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Showing Extract Table Using Examples button" class="gallery-image" data-flex-basis="319px" data-flex-grow="133" height="511" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-bi-import-text-using-examples/TextImport_002.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;When the button is clicked a new dialog opens a dialog box with a data preview showing and an empty table. This means we need to enter in some answers into the table so a pattern can be calculated how to transform the data. When you start to type in a value it will suggest possible values.&lt;/p&gt;
&lt;p&gt;A column is renamed by double clicking on the column title and entering a new name.&lt;/p&gt;
&lt;p&gt;&lt;img alt="entering first column into import text file using example\n" class="gallery-image" data-flex-basis="321px" data-flex-grow="133" height="508" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-bi-import-text-using-examples/TextImport_003.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;With only one value it cannot calculate the pattern required. When I have added more values it can determine the pattern. It often needs less values in future columns. In this example 2 dates were needed for the first column and then only one value for the remaining columns.&lt;/p&gt;
&lt;p&gt;&lt;img alt="showing stages of adding example values in\n" class="gallery-image" data-flex-basis="1191px" data-flex-grow="496" height="137" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-bi-import-text-using-examples/TextImport_004.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;In the above example the Distance column was only part of the row of data. It calculates the ways to split the data. After this and all the columns have been added, we click Transform Data to see the Power Query steps that the import feature have been written.&lt;/p&gt;
&lt;h3 id="looking-at-the-power-query"&gt;Looking at the Power Query
&lt;/h3&gt;&lt;p&gt;So the magic behind this new feature is it writes a 10+ step query for you with a pretty good pattern for doing this. So the 5 steps after the source are working out how many rows make a block of data and grouping that data into one block. The next chunk of steps are splitting that block into separate columns.&lt;/p&gt;
&lt;p&gt;&lt;img alt="look at the power query created by import text by using examples" class="gallery-image" data-flex-basis="511px" data-flex-grow="213" height="319" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-bi-import-text-using-examples/TextImport_005.png" width="680"&gt;&lt;/p&gt;
&lt;h3 id="how-does-import-text-using-example-decide-the-block-length"&gt;How does import text using example decide the block length?
&lt;/h3&gt;&lt;p&gt;This for me is the clever part. Step three where it adds the conditional column is the crucial step. So I looked at three slightly different versions of the text file to see how it changes.&lt;/p&gt;
&lt;h4 id="example-1"&gt;Example 1
&lt;/h4&gt;&lt;p&gt;&lt;img alt="step 3 when starting with a date" class="gallery-image" data-flex-basis="497px" data-flex-grow="207" height="328" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-bi-import-text-using-examples/TextImport_006.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;In this example each of the blocks start with a date, which means the line starts with a number. Step 3 uses a test to see if in position 0 there is a number. So for this example, we could have as many rows as we liked in a block as long as the first row is the only row starting with a number.&lt;/p&gt;
&lt;h4 id="example-2"&gt;Example 2
&lt;/h4&gt;&lt;p&gt;&lt;img alt="example 2 starting with the word date." class="gallery-image" data-flex-basis="497px" data-flex-grow="207" height="328" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-bi-import-text-using-examples/TextImport_007.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;In this example the blocks start with the word “Date”. Step 3 looks for the line starting with “Date” as the first line. This is pattern is a great one as it works fine for different length blocks as long as the only lines starting with “Date”.&lt;/p&gt;
&lt;h4 id="example-3"&gt;Example 3
&lt;/h4&gt;&lt;p&gt;&lt;img alt="example 3 when there is no obvious pattern" class="gallery-image" data-flex-basis="499px" data-flex-grow="207" height="327" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-bi-import-text-using-examples/TextImport_008.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;In this example there is no easy way to identify the top row of the data. This means the only pattern it can determine is by length of the block so it guesses 4. This works if the data is in same length blocks.&lt;/p&gt;
&lt;h3 id="conclusion"&gt;Conclusion
&lt;/h3&gt;&lt;p&gt;I like the new import text using examples feature and I can see places where it will get used. I hope it gets progressed to recognise more patterns and be more flexible. When it works it stops the report writer needing to put together the Power Query steps to do the transform.&lt;/p&gt;
&lt;h2 id="more-power-bi-posts"&gt;More Power BI Posts
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-bi-conditional-formatting-update/" target="_blank" rel="noopener"
 &gt;Conditional Formatting Update&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-bi-data-refresh-date/" target="_blank" rel="noopener"
 &gt;Data Refresh Date&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-bi-inactive-relationships-in-a-measure/" target="_blank" rel="noopener"
 &gt;Using Inactive Relationships in a Measure&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-bi-dax-crossfilter-function/" target="_blank" rel="noopener"
 &gt;DAX CrossFilter Function&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-bi-coalesce-function-to-remove-blanks/" target="_blank" rel="noopener"
 &gt;COALESCE Function to Remove Blanks&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-bi-personalize-visuals/" target="_blank" rel="noopener"
 &gt;Personalize Visuals&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-bi-gradient-legends/" target="_blank" rel="noopener"
 &gt;Gradient Legends&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-bi-endorse-a-dataset/" target="_blank" rel="noopener"
 &gt;Endorse a Dataset as Promoted or Certified&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-bi-qa-synonyms-update/" target="_blank" rel="noopener"
 &gt;Q&amp;amp;A Synonyms Update&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-bi-import-text-using-examples/" target="_blank" rel="noopener"
 &gt;Import Text Using Examples&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/paginated-report-resources/" target="_blank" rel="noopener"
 &gt;Paginated Report Resources&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/refreshing-datasets-automatically-with-dataflow/" target="_blank" rel="noopener"
 &gt;Refreshing Datasets Automatically with Power BI Dataflows&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/charticulator-simple-custom-chart/" target="_blank" rel="noopener"
 &gt;Charticulator&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-bi-dataverse-connector-july-2022-update/" target="_blank" rel="noopener"
 &gt;Dataverse Connector – July 2022 Update&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-bi-dataverse-choices-and-choice-column/" target="_blank" rel="noopener"
 &gt;Dataverse Choice Columns&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-bi-switch-dataverse-tenancy/" target="_blank" rel="noopener"
 &gt;Switch Dataverse Tenancy&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-bi-connecting-to-google-analytics/" target="_blank" rel="noopener"
 &gt;Connecting to Google Analytics&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-bi-take-over-a-dataset/" target="_blank" rel="noopener"
 &gt;Take Over a Dataset&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/export-data-from-power-bi-visuals/" target="_blank" rel="noopener"
 &gt;Export Data from Power BI Visuals&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-bi-embed-a-paginated-report/" target="_blank" rel="noopener"
 &gt;Embed a Paginated Report&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/using-sql-on-dataverse-for-power-bi/" target="_blank" rel="noopener"
 &gt;Using SQL on Dataverse for Power BI&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-platform-solution-and-power-bi-part-1/" target="_blank" rel="noopener"
 &gt;Power Platform Solution and Power BI Series&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-bi-creating-a-custom-smart-narrative/" target="_blank" rel="noopener"
 &gt;Creating a Custom Smart Narrative&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-automate-button-in-a-power-bi-report/" target="_blank" rel="noopener"
 &gt;Power Automate Button in a Power BI Report&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="power-bi-series"&gt;Power BI Series
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/svg-in-power-bi-part-1-svg-basics/" target="_blank" rel="noopener"
 &gt;SVG in Power BI series&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-bi-connecting-to-project-online/" target="_blank" rel="noopener"
 &gt;Power BI and Project Online series&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-bi-slicers-introduction/" target="_blank" rel="noopener"
 &gt;Slicers series&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-bi-create-a-dataflow/" target="_blank" rel="noopener"
 &gt;Dataflow series&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/svg-in-power-bi-part-1-svg-basics/" target="_blank" rel="noopener"
 &gt;Power BI SVG series&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-automate-and-power-bi-rest-api/" target="_blank" rel="noopener"
 &gt;Power Automate and Power BI Rest API series&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/devops-data-into-power-bi/" target="_blank" rel="noopener"
 &gt;Power BI and DevOps series&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Power Query – Replace Values for Whole Table</title><link>https://hatfullofdata.blog/power-query-replace-values-for-whole-table/</link><pubDate>Wed, 15 Apr 2020 15:40:57 +0000</pubDate><guid>https://hatfullofdata.blog/power-query-replace-values-for-whole-table/</guid><description>&lt;img src="https://hatfullofdata.blog/power-query-replace-values-for-whole-table/cover.png" alt="Featured image of post Power Query – Replace Values for Whole Table" /&gt;&lt;p&gt;Today’s challenge was to replace the values on all the columns in the query when I know the columns will change so I don’t want to name them.&lt;/p&gt;
&lt;h3 id="quick-answer"&gt;Quick Answer
&lt;/h3&gt;&lt;p&gt;For those who don’t want the long explanation. Do replace values on at least one column to get the replace step. In the example below the previous step is #’Changed Type’ and the columns are Jan 20 and Feb 20.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-xml" data-lang="xml"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;= Table.ReplaceValue(#&amp;#34;Changed Type&amp;#34;,null,0,Replacer.ReplaceValue,{&amp;#34;Jan 20&amp;#34;, &amp;#34;Feb 20&amp;#34;})
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;Remove the {…} section and replace it with Table.ColumnNames(Previous Step)&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-xml" data-lang="xml"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;= Table.ReplaceValue(#&amp;#34;Changed Type&amp;#34;,null,0,Replacer.ReplaceValue,Table.ColumnNames(#&amp;#34;Changed Type&amp;#34;))
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;&lt;img alt="Replace values with named columns" class="gallery-image" data-flex-basis="1052px" data-flex-grow="438" height="155" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-replace-values-for-whole-table/ReplaceAll_001.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt="Replace values on all columns" class="gallery-image" data-flex-basis="1110px" data-flex-grow="462" height="147" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-replace-values-for-whole-table/ReplaceAll_002.png" width="680"&gt;&lt;/p&gt;
&lt;h3 id="longer-answer"&gt;Longer Answer
&lt;/h3&gt;&lt;p&gt;Replace values is a powerful tool for coping with null values and errors. When you select multiple columns and select Replaces values a step is written in that includes a list of column names, eg {“column1″,”column2″,”column3”}. If your data structure changes that step will either break because a column is missing or will not include a new column.&lt;/p&gt;
&lt;p&gt;The function Table.ColumnNames(StepName) returns a list of column names from the named step. To see this result and filter the list right click on a step and select Insert Step After. Edit the new step to be Table.ColumnNames(StepName)&lt;/p&gt;
&lt;p&gt;&lt;img alt="Column name list" class="gallery-image" data-flex-basis="1046px" data-flex-grow="435" height="156" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-replace-values-for-whole-table/ReplaceAll_003.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;The list of column names can be filtered, even though there is no drop down arrow. Right click on the value to remove and select Remove Item.&lt;/p&gt;
&lt;p&gt;&lt;img alt="column name list" class="gallery-image" data-flex-basis="680px" data-flex-grow="283" height="240" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-replace-values-for-whole-table/ReplaceAll_004.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;If I then rename this step to ColumnNames I can then use that step name in a replace step.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Replace Values using named step" class="gallery-image" data-flex-basis="943px" data-flex-grow="393" height="173" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-replace-values-for-whole-table/ReplaceAll_005.png" width="680"&gt;&lt;/p&gt;
&lt;h3 id="note"&gt;Note
&lt;/h3&gt;&lt;p&gt;Yes the Replaced Value step is refering to a calculation that comes after it (yup its wacky). If you reorder the steps by dragging them Power Query will decide to change the code so steps refer to the new previous step.&lt;/p&gt;
&lt;h3 id="references"&gt;References
&lt;/h3&gt;&lt;p&gt;&lt;a class="link" href="https://docs.microsoft.com/en-us/powerquery-m/table-columnnames" target="_blank" rel="noopener"
 &gt;Microsoft Docs Table.ColumnNames&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="more-power-query-posts"&gt;More Power Query Posts
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-handwritten-function/" target="_blank" rel="noopener"
 &gt;Custom Handwritten Function&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-multi-step-function/" target="_blank" rel="noopener"
 &gt;Multi-step Function&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-replace-values-for-whole-table/" target="_blank" rel="noopener"
 &gt;Replace Values for Whole Table&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-ai-insights-error/" target="_blank" rel="noopener"
 &gt;AI Insights Error&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/excel-power-query-vba-to-edit-a-parameter-value/" target="_blank" rel="noopener"
 &gt;VBA to Edit a Parameter Value&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-dynamic-data-source-and-web-content/" target="_blank" rel="noopener"
 &gt;Dynamic Data Source and Web.Contents()&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-get-previous-row-data/" target="_blank" rel="noopener"
 &gt;Get Previous Row Data&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-creating-new-parameters/" target="_blank" rel="noopener"
 &gt;Creating New Parameters&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-fixing-missing-columns-dynamically/" target="_blank" rel="noopener"
 &gt;Fixing Missing Columns Dynamically&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-handling-null-values/" target="_blank" rel="noopener"
 &gt;Handling Null Values Properly&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Power Query Functions – SQL Procedures</title><link>https://hatfullofdata.blog/power-query-execute-sql-procedure/</link><pubDate>Sun, 06 Oct 2019 17:25:02 +0000</pubDate><guid>https://hatfullofdata.blog/power-query-execute-sql-procedure/</guid><description>&lt;p&gt;This is the fourth post in my series regarding writing custom functions in Power Query. This post describes how to create a function that will execute an SQL procedure, passing in a parameter.&lt;/p&gt;
&lt;p&gt;This series is to support my sessions at Data Relay 2019 and will cover the topics in the session.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-handwritten-function/" target="_blank" rel="noopener"
 &gt;Handwritten Functions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-multi-step-function/" target="_blank" rel="noopener"
 &gt;Multi-step Functions and Parameters&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-fetch-web-data/" target="_blank" rel="noopener"
 &gt;Using functions to fetch web data&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-function-to-execute-a-procedure/" target="_blank" rel="noopener"
 &gt;Executing SQL procedures from functions&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="stored-procedure"&gt;Stored Procedure
&lt;/h3&gt;&lt;p&gt;My database contains a very simple procedure that given a cost center returns all the expense records.&lt;/p&gt;
&lt;p&gt;&lt;img alt="stored procedure" class="gallery-image" data-flex-basis="886px" data-flex-grow="369" height="184" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-execute-sql-procedure/customfunc_401.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;It can be run with the following SQL&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-xml" data-lang="xml"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;EXECUTE dbo.GetExpenses @CostCenter=&amp;#34;B01&amp;#34;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h3 id="create-initial-query"&gt;Create Initial Query
&lt;/h3&gt;&lt;p&gt;The initial query will excute a procedure with a fixed parameter being passed to the procedure using the SQL in the previous section.&lt;/p&gt;
&lt;p&gt;Add a new source of SQL Server. Fill in the Server and Database and then click on Advanced options to reveal the SQL statement box where you enter the SQL statement to execute the procedure.&lt;/p&gt;
&lt;p&gt;&lt;img alt="entering in SQL procedure statement" class="gallery-image" data-flex-basis="232px" data-flex-grow="96" height="703" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-execute-sql-procedure/customfunc_402.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;Click OK to and then Transform Data to edit the query. When the query opens it has only one step called Source.&lt;/p&gt;
&lt;h3 id="create-parameter-and-function"&gt;Create Parameter and Function
&lt;/h3&gt;&lt;p&gt;After creating the query, we need to create a parameter to store the value to pass into the procedure. So from the Home ribbon tab select Manage Parameters – New Parameter. You need to give the parameter a name and the type Text and lastly a value.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Creating parameter" class="gallery-image" data-flex-basis="291px" data-flex-grow="121" height="559" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-execute-sql-procedure/customfunc_403.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;Then we need to edit the Source step of the query to use the above parameter. Remember to get the number of ” correct, you need 2 ” if the ” is inside a string.&lt;/p&gt;
&lt;p&gt;&lt;img alt="inserting parameter" class="gallery-image" data-flex-basis="761px" data-flex-grow="317" height="214" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-execute-sql-procedure/customfunc_404-1.png" width="679"&gt;&lt;/p&gt;
&lt;p&gt;When you alter the query you might get a prompt asking for permission to run the query. For now let it run the query, further on in this post I will discuss this further.&lt;/p&gt;
&lt;p&gt;After we have included the parameter into the query it is ready to be converted into a function, just as we have done in previous posts. Right click on the query and select Create Function, then enter in a name for the function. As before the query and parameter get moved into a group with the new function.&lt;/p&gt;
&lt;p&gt;&lt;img alt="function group" class="gallery-image" data-flex-basis="309px" data-flex-grow="128" height="392" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-execute-sql-procedure/customfunc_405.png" width="505"&gt;&lt;/p&gt;
&lt;p&gt;This function can now be invoked on a table of cost centers to give all the related expenses.&lt;/p&gt;
&lt;p&gt;&lt;img alt="cost center table" class="gallery-image" data-flex-basis="391px" data-flex-grow="163" height="273" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-execute-sql-procedure/customfunc_409.png" width="445"&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt="expanded to expenses" class="gallery-image" data-flex-basis="872px" data-flex-grow="363" height="187" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-execute-sql-procedure/customfunc_410.png" width="680"&gt;&lt;/p&gt;
&lt;h3 id="changing-security"&gt;Changing Security
&lt;/h3&gt;&lt;p&gt;When you invoke the query you will get a prompt asking for permission to run a native database query. This is a caution to warn you that some SQL is being run which could makes changes to the database.&lt;/p&gt;
&lt;p&gt;&lt;img alt="permission is required" class="gallery-image" data-flex-basis="1457px" data-flex-grow="607" height="112" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-execute-sql-procedure/customfunc_406.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;When you click on Edit Permission you will be shown the SQL that is going to be executed, so you can check it carefully.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Native Database Query" class="gallery-image" data-flex-basis="329px" data-flex-grow="137" height="495" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-execute-sql-procedure/customfunc_408.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;This request for permission can be turned off. Click on File and then Options, and then Security. The first option in the dialog is regarding Native Database Queries, by default the require approval is ticked. Be aware this changes it for all queries.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Turn off requiring approval" class="gallery-image" data-flex-basis="236px" data-flex-grow="98" height="691" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-execute-sql-procedure/customfunc_407.png" width="680"&gt;&lt;/p&gt;
&lt;h3 id="conclusion"&gt;Conclusion
&lt;/h3&gt;&lt;p&gt;SQL procedures are a great way to make use of the dba to build the queries you need to fetch the data using your parameters. Obviously this will require many biscuits for the dba. I think there is plenty of scope for functions that execute an SQL procedure.&lt;/p&gt;
&lt;h3 id="resources"&gt;Resources
&lt;/h3&gt;&lt;p&gt;I am not the first, and hopefully not the last to write blog posts on writing functions in M for Power Query. Here are a list of the resources I found useful. (If you know of any good ones I’ve missed please let me know!)&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a class="link" href="https://blog.crossjoin.co.uk/2016/05/15/creating-m-functions-from-parameterised-queries-in-power-bi/" target="_blank" rel="noopener"
 &gt;Chris Webb’s Creating M Functions From Parameterised Queries In Power BI&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://skillsmatter.com/skillscasts/10210-working-with-parameters-and-functions-in-power-query-excel-and-power-bi" target="_blank" rel="noopener"
 &gt;Chris Webb presenting at Skills Matter on Working with Parameters and Functions in Power Query/Excel and Power BI&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://ssbi-blog.de/writing-documentation-for-custom-m-functions/" target="_blank" rel="noopener"
 &gt;Lars Schreiber’s Writing documentation for custom M-functions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://bengribaudo.com/blog/2017/11/17/4107/power-query-m-primer-part1-introduction-simple-expressions-let" target="_blank" rel="noopener"
 &gt;Ben Gribaudo’s Power Query M Primer&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Power Query Functions – Fetch web data</title><link>https://hatfullofdata.blog/power-query-fetch-web-data/</link><pubDate>Sat, 05 Oct 2019 17:33:54 +0000</pubDate><guid>https://hatfullofdata.blog/power-query-fetch-web-data/</guid><description>&lt;p&gt;This is the third post in my Custom functions in Power Query series. Often web based data is split across multiple pages using the url to select the data. This post describes how to build a function that populates the parts of the url so multiple pages can be used to fetch web data can be combined.&lt;/p&gt;
&lt;p&gt;This series is to support my sessions at Data Relay 2019 and will cover the topics in the session.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-handwritten-function/" target="_blank" rel="noopener"
 &gt;Handwritten Functions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-multi-step-function/" target="_blank" rel="noopener"
 &gt;Multi-step Functions and Parameters&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-fetch-web-data/" target="_blank" rel="noopener"
 &gt;Using functions to fetch web data&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-function-to-execute-a-procedure/" target="_blank" rel="noopener"
 &gt;Executing SQL procedures from functions&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="initial-query-to-fetch-web-data"&gt;Initial Query to Fetch Web Data
&lt;/h3&gt;&lt;p&gt;We start the whole process by fetching data from one page of the web site. for my example in this post I am going to use data from Formula 1 racing. The page I am using as my initial page if for the first race in 2019 in Australia. The url is&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-xml" data-lang="xml"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;https://www.formula1.com/en/results.html/2019/races/1000/australia/race-result.html
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;&lt;img alt="Web page" class="gallery-image" data-flex-basis="413px" data-flex-grow="172" height="395" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-fetch-web-data/customfunc_301.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;I’ve used table by example to extract the data I need. (There is a blog post coming cover table by example.) So I end up with three step query.&lt;/p&gt;
&lt;p&gt;&lt;img alt="3 step query to fetch web data" class="gallery-image" data-flex-basis="766px" data-flex-grow="319" height="213" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-fetch-web-data/customfunc_302.png" width="680"&gt;&lt;/p&gt;
&lt;h3 id="adding-first-parameter-to-web-query"&gt;Adding First Parameter to Web Query
&lt;/h3&gt;&lt;p&gt;The next step is to replace parts of the url with parameters in the Source step. If we look at the url it has three parts that could be made into parameters. The Race Year, Race Number and Race Name. For the the first race in 2019 the values would be :&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;RaceYear – 2019&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;RaceNumber – 1000&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;RaceName – Australia&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The first parameter needs to be created before you change the Source step. From the Home ribbon tab, select Manage Parameters, New Parameter. Then you need to enter in a name for the parameter, a type of text and the current value. The type has to be text for the parameter to get included in the next step. Finally click OK to create the parameter.&lt;/p&gt;
&lt;p&gt;&lt;img alt="creating first parameter" class="gallery-image" data-flex-basis="299px" data-flex-grow="124" height="545" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-fetch-web-data/customfunc_303-1.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;Now we can start to change the source step in the query. Start by clicking on the cog wheel of the Source step which will open a dialog. Then click on Advanced to give the option to break the URL into parts. You can click on Add Part to add extra rows. The first drop down allows offers Text or Parameter. If you don’t get the drop down on the left it means you don’t have any text parameters.&lt;/p&gt;
&lt;p&gt;&lt;img alt="adding first parameter to source" class="gallery-image" data-flex-basis="380px" data-flex-grow="158" height="429" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-fetch-web-data/customfunc_304.png" width="680"&gt;&lt;/p&gt;
&lt;h3 id="adding-extra-parameters"&gt;Adding Extra Parameters
&lt;/h3&gt;&lt;p&gt;Earlier I stated that the url had three parts that could be put into parameters. We can create and add the new parameters inside the From Web dialog. Click Add Part to add a new row. From the new drop down select New Parameter and enter in the details of your new parameter, remember they need to be text.&lt;/p&gt;
&lt;p&gt;&lt;img alt="adding more parameters" class="gallery-image" data-flex-basis="318px" data-flex-grow="132" height="513" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-fetch-web-data/customfunc_305.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;When you have finished adding new parameter and breaking the url up, click OK to save your changes. Check you still get the correct data from your query.&lt;/p&gt;
&lt;h3 id="creating-function-from-query"&gt;Creating Function from Query
&lt;/h3&gt;&lt;p&gt;We now have a query that can fetch web data which we now need to use to build a function from it..&lt;/p&gt;
&lt;p&gt;Right click on your query and select Create Function. Type in a name for the query.&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="608px" data-flex-grow="253" height="268" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-fetch-web-data/customfunc_306.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;The query and the parameters are all moved into a group and a function is created. If any changes are required in the function, the changes need to be made to the query. If changes are made to the function it will break the link with the query.&lt;/p&gt;
&lt;p&gt;&lt;img alt="newly created function" class="gallery-image" data-flex-basis="652px" data-flex-grow="272" height="250" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-fetch-web-data/customfunc_307.png" width="680"&gt;&lt;/p&gt;
&lt;h3 id="fetch-web-data-from-multiple-pages"&gt;Fetch web data from Multiple pages
&lt;/h3&gt;&lt;p&gt;I have a list of races with year, number and name in an Excel file which I load into a new query. In order for the function to work the RaceYear and RaceNumber columns need to be text.&lt;/p&gt;
&lt;p&gt;&lt;img alt="race data" class="gallery-image" data-flex-basis="336px" data-flex-grow="140" height="485" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-fetch-web-data/customfunc_308.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;From the Add Column click Invoke Function. Type in a New column name and select the function in the drop. Then the parameters for the function will appear. Change the first drop down for each parameter to Column Name and select the column.&lt;/p&gt;
&lt;p&gt;&lt;img alt="invoke function" class="gallery-image" data-flex-basis="420px" data-flex-grow="175" height="388" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-fetch-web-data/customfunc_309.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;When you click the OK button the function will run for every row of the table. This will create a column that contains tables. Click on the icon in the top right of the new column. In the dialog box that appears it will show the columns in the table. I un-ticked the Use original column name as prefix to keep the column names short,&lt;/p&gt;
&lt;p&gt;&lt;img alt="expand table column" class="gallery-image" data-flex-basis="580px" data-flex-grow="241" height="281" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-fetch-web-data/customfunc_311.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;After OK is clicked the column of tables is replaced by the three columns from the table.&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="1157px" data-flex-grow="482" height="141" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-fetch-web-data/customfunc_310.png" width="680"&gt;&lt;/p&gt;
&lt;h3 id="conclusion"&gt;Conclusion
&lt;/h3&gt;&lt;p&gt;If your data is web based this provides an efficient method of fetching web data into one table only using a few queries and a function.&lt;/p&gt;
&lt;h3 id="resources"&gt;Resources
&lt;/h3&gt;&lt;p&gt;I am not the first, and hopefully not the last to write blog posts on writing functions in M for Power Query. Here are a list of the resources I found useful. (If you know of any good ones I’ve missed please let me know!)&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a class="link" href="https://blog.crossjoin.co.uk/2016/05/15/creating-m-functions-from-parameterised-queries-in-power-bi/" target="_blank" rel="noopener"
 &gt;Chris Webb’s Creating M Functions From Parameterised Queries In Power BI&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://skillsmatter.com/skillscasts/10210-working-with-parameters-and-functions-in-power-query-excel-and-power-bi" target="_blank" rel="noopener"
 &gt;Chris Webb presenting at Skills Matter on Working with Parameters and Functions in Power Query/Excel and Power BI&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://ssbi-blog.de/writing-documentation-for-custom-m-functions/" target="_blank" rel="noopener"
 &gt;Lars Schreiber’s Writing documentation for custom M-functions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://bengribaudo.com/blog/2017/11/17/4107/power-query-m-primer-part1-introduction-simple-expressions-let" target="_blank" rel="noopener"
 &gt;Ben Gribaudo’s Power Query M Primer&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Power Query – Multi-step Function</title><link>https://hatfullofdata.blog/power-query-multi-step-function/</link><pubDate>Sun, 29 Sep 2019 17:28:40 +0000</pubDate><guid>https://hatfullofdata.blog/power-query-multi-step-function/</guid><description>&lt;p&gt;This is the second post in my Writing Functions in M series. This post describes how to build a multi-step function in M to allow for a more complex function. We will create a parameter to base the calculations on and then build a function.&lt;/p&gt;
&lt;p&gt;This series is to support my sessions at Data Relay 2019 and will cover the topics in the session.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-handwritten-function/" target="_blank" rel="noopener"
 &gt;Handwritten Functions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-multi-step-function/" target="_blank" rel="noopener"
 &gt;Multi-step Functions and Parameters&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-fetch-web-data/" target="_blank" rel="noopener"
 &gt;Using functions to fetch web data&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-function-to-execute-a-procedure/" target="_blank" rel="noopener"
 &gt;Executing SQL procedures from functions&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="introduction"&gt;Introduction
&lt;/h3&gt;&lt;p&gt;In the first post of this series we built a different functions and all of them were a single step calculation. Some functions need to be made up of multiple steps. The example we will use in this example is to calculate the next working day based on Saturday and Sunday being non-working days.&lt;/p&gt;
&lt;p&gt;In simple terms the calculation is simple, if today is a Friday add 3 days, Saturday add 2 days, otherwise just add one day. I realise this could be done with a nested if, I’m using this example as a nice easy one to walk through.&lt;/p&gt;
&lt;h3 id="creating-a-parameter"&gt;Creating a Parameter
&lt;/h3&gt;&lt;p&gt;In order to make writing functions easier we create parameters to store the values passed into our multi-step function. This function will only require a single parameter to store the date which we want to calculate the next working day.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Select New Parameter" class="gallery-image" data-flex-basis="476px" data-flex-grow="198" height="342" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-multi-step-function/customfunc_205.png" width="679"&gt;&lt;/p&gt;
&lt;p&gt;From the Home ribbon tab, select Manage Parameters and the New Parameter. Then enter in a name for the parameter, e.g. ThisDate and select a Type, e.g. Date. Finally add in a Current Value and press OK.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Setting up parameter" class="gallery-image" data-flex-basis="300px" data-flex-grow="125" height="544" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-multi-step-function/customfunc_206.png" width="680"&gt;&lt;/p&gt;
&lt;h3 id="calculation-steps"&gt;Calculation Steps
&lt;/h3&gt;&lt;p&gt;Now we can start creating the function in with a blank query which I have renamed to NextWorkDay. This first step I am going to make equal to the parameter we created, ThisDate. (Remember the = , I have forgotten it too many times)&lt;/p&gt;
&lt;p&gt;&lt;img alt="Using parameter in the step" class="gallery-image" data-flex-basis="735px" data-flex-grow="306" height="222" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-multi-step-function/customfunc_201-1.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;The first calculation step I am going to add is to calculate the day of the week of the given date. This can be done from the ribbon tab DateTime Tools. Select Date – Day – Day of Week and a new step gets added using the function Date.DayOfWeek.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Using ribbon to add new step." class="gallery-image" data-flex-basis="283px" data-flex-grow="118" height="575" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-multi-step-function/customfunc_202.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;Rename the step just added to something shorter, e.g. WeekDay.&lt;/p&gt;
&lt;p&gt;The next step will be to calculate how many days to add to the date. You can add a new step by clicking on the fx next to the formula bar.&lt;/p&gt;
&lt;p&gt;We use an If statement based on the previous step. There is no quick way to add this so you will need to type in formula. The editor does now have intellisense so you should get some assistance in typing in names.&lt;/p&gt;
&lt;p&gt;&lt;img alt="intellisense in action" class="gallery-image" data-flex-basis="1174px" data-flex-grow="489" height="139" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-multi-step-function/customfunc_203.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;Using the logic described in the second paragraph of the introduction the complete formula is&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-xml" data-lang="xml"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;= if WeekDay=4 then 3 else 
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; if WeekDay=5 then 2 
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; else 1
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;And I rename the step to be DaysToAdd to make the next step easier and it should return 1,2 or 3. You can click on any step and see the result, for example clicking on Source gives you the date.&lt;/p&gt;
&lt;p&gt;So the final step is to use Source and DaysToAdd to calculate the next working day. We use the function Date.AddDays which requires 2 parameters, a date which is Source and the number of days to add which is DaysToAdd.&lt;/p&gt;
&lt;p&gt;&lt;img alt="FInal calculation step AddDays\n" class="gallery-image" data-flex-basis="562px" data-flex-grow="234" height="290" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-multi-step-function/customfunc_204.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;We can change the date in the parameter ThisDate to test different dates and check the calculation logic. Now we are ready to convert this to function.&lt;/p&gt;
&lt;h3 id="creating-the-multi-step-function"&gt;Creating the Multi-step Function
&lt;/h3&gt;&lt;p&gt;Power Query includes a quick method to convert a query that uses a parameter into a function.&lt;/p&gt;
&lt;p&gt;Right click on the NextWorkDay function and from the menu select Create Function. Enter in a name for the function in the Create Function dialog and press OK.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Create function" class="gallery-image" data-flex-basis="593px" data-flex-grow="247" height="275" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-multi-step-function/customfunc_207.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;This will move both NextWorkDay query and ThisDate parameter into a separate group with the new function. If you want to edit the function you need to edit NextWorkDay and it will update the function fnNextWorkDay. You can edit the function directly but it will break the connection with NextWorkDay so debugging will be harder and is not recommended.&lt;/p&gt;
&lt;h3 id="using-your-multi-step-function"&gt;Using your Multi-step Function
&lt;/h3&gt;&lt;p&gt;&lt;img alt="Final Function" class="gallery-image" data-flex-basis="832px" data-flex-grow="346" height="196" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-multi-step-function/customfunc_208.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;The function can now be used in any query in your report by clicking Invoke Custom Column from the Add Column ribbon.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Invoking the multi-step function" class="gallery-image" data-flex-basis="540px" data-flex-grow="225" height="302" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-multi-step-function/customfunc_209.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt="final result" class="gallery-image" data-flex-basis="471px" data-flex-grow="196" height="346" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-multi-step-function/customfunc_210.png" width="680"&gt;&lt;/p&gt;
&lt;h3 id="conclusion"&gt;Conclusion
&lt;/h3&gt;&lt;p&gt;The quick Create Function ability makes creating and editing the functions much easier. It makes building functions worth doing and not a huge development process.&lt;/p&gt;
&lt;h3 id="resources"&gt;Resources
&lt;/h3&gt;&lt;p&gt;I am not the first, and hopefully not the last to write blog posts on writing functions in M for Power Query. Here are a list of the resources I found useful. (If you know of any good ones I’ve missed please let me know!)&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a class="link" href="https://blog.crossjoin.co.uk/2016/05/15/creating-m-functions-from-parameterised-queries-in-power-bi/" target="_blank" rel="noopener"
 &gt;Chris Webb’s Creating M Functions From Parameterised Queries In Power BI&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://skillsmatter.com/skillscasts/10210-working-with-parameters-and-functions-in-power-query-excel-and-power-bi" target="_blank" rel="noopener"
 &gt;Chris Webb presenting at Skills Matter on Working with Parameters and Functions in Power Query/Excel and Power BI&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://ssbi-blog.de/writing-documentation-for-custom-m-functions/" target="_blank" rel="noopener"
 &gt;Lars Schreiber’s Writing documentation for custom M-functions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://bengribaudo.com/blog/2017/11/17/4107/power-query-m-primer-part1-introduction-simple-expressions-let" target="_blank" rel="noopener"
 &gt;Ben Gribaudo’s Power Query M Primer&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Power Query – Custom Handwritten Function</title><link>https://hatfullofdata.blog/power-query-handwritten-function/</link><pubDate>Fri, 27 Sep 2019 17:50:43 +0000</pubDate><guid>https://hatfullofdata.blog/power-query-handwritten-function/</guid><description>&lt;h3 id="introduction"&gt;Introduction
&lt;/h3&gt;&lt;p&gt;In this post I will write a simple custom handwritten function to perform a calculation based off a parameter. We will then expand that function to include data types and optional parameters.&lt;/p&gt;
&lt;p&gt;For those who are new to programming a function is a piece of code that returns some data, which could be a single value, record or table of data. If you ever need to duplicate a calculation more that twice then it is worth considering writing a function.&lt;/p&gt;
&lt;p&gt;This series is to support my sessions at Data Relay 2019 and will cover the topics in the session.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-handwritten-function/" target="_blank" rel="noopener"
 &gt;Handwritten Functions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-multi-step-function/" target="_blank" rel="noopener"
 &gt;Multi-step Functions and Parameters&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-fetch-web-data/" target="_blank" rel="noopener"
 &gt;Using functions to fetch web data&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-function-to-execute-a-procedure/" target="_blank" rel="noopener"
 &gt;Executing SQL procedures from functions&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="the-hello-world-function"&gt;The Hello World function
&lt;/h3&gt;&lt;p&gt;Every programming 101 course includes some sort of Hello World function as the introduction. I would hate to break tradition.&lt;/p&gt;
&lt;p&gt;In Power Query we add a Blank Query and we get a blank formula bar. Enter in the following code.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-xml" data-lang="xml"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;= () =&amp;gt; &amp;#34;Hello World&amp;#34;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;When you press return the blank query will be converted into a custom function. It gains an fx in the Queries list. It can be renamed in the Query Settings just like a query.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Hello World Function definition" class="gallery-image" data-flex-basis="872px" data-flex-grow="363" height="187" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-handwritten-function/customfunc_101.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;The function can then be used to create a column in a table. From the Add Column ribbon tab, click Invoke Custom Function. Enter in a name for the new column and select the new function from the list.&lt;/p&gt;
&lt;p&gt;&lt;img alt="invoking Hello World function" class="gallery-image" data-flex-basis="587px" data-flex-grow="244" height="278" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-handwritten-function/customfunc_102.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;This will add a column to your data. This example is a pretty useless example of just Hello World but tradition has been done and we are now ready to move to the next stage.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Data with Hello World column added" class="gallery-image" data-flex-basis="712px" data-flex-grow="296" height="229" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-handwritten-function/customfunc_103.png" width="680"&gt;&lt;/p&gt;
&lt;h3 id="adding-a-parameter-to-our-handwritten-function"&gt;Adding a parameter to our Handwritten Function
&lt;/h3&gt;&lt;p&gt;The Hello World function just returned one value, which has a very limited use. So now we will add a parameter to our function and use that value to change our result to include that message.&lt;/p&gt;
&lt;p&gt;Parameter definitions go between the brackets in the definition. I’ve updated the function to be this code and on pressing return will now have details of the parameter required.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-xml" data-lang="xml"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;= (GuestName) =&amp;gt; &amp;#34;Hello &amp;#34; &lt;span class="err"&gt;&amp;amp;&lt;/span&gt; GuestName
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;&lt;img alt="Hello function with parameter" class="gallery-image" data-flex-basis="473px" data-flex-grow="197" height="345" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-handwritten-function/customfunc_104.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;If the function is being used anywhere you will need to modify the function. You can either remove and re-add the function or click the cog wheel on the relevant step. There are two possible windows you will get on clicking the cog wheel.&lt;/p&gt;
&lt;p&gt;&lt;img alt="invoke handwritten function" class="gallery-image" data-flex-basis="542px" data-flex-grow="225" height="301" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-handwritten-function/customfunc_105.png" width="680"&gt;Editing a working custom function column&lt;/p&gt;
&lt;p&gt;&lt;img alt="edit function" class="gallery-image" data-flex-basis="381px" data-flex-grow="158" height="428" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-handwritten-function/customfunc_106.png" width="680"&gt;Editing a custom function column that returns an error&lt;/p&gt;
&lt;p&gt;Your table will now show the result of your updated handwritten function.&lt;/p&gt;
&lt;p&gt;&lt;img alt="results" class="gallery-image" data-flex-basis="803px" data-flex-grow="334" height="203" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-handwritten-function/customfunc_107.png" width="680"&gt;&lt;/p&gt;
&lt;h3 id="data-types"&gt;Data Types
&lt;/h3&gt;&lt;p&gt;I can feel my degree lectures turning in their graves as the above function has no data types specified. You can find many online references as to why adding data types into code is good practice, for this post please accept it is good practice and good practice usually leads to cleaner and faster code.&lt;/p&gt;
&lt;p&gt;The fnHelloWorld has a parameter that is text and it returns some text. So we can adjust the code with “as text” after the GuestName parameter and also after the ). It won’t change the function much but it will in the long term be a great habit to adopt.&lt;/p&gt;
&lt;p&gt;&lt;img alt="data types added" class="gallery-image" data-flex-basis="547px" data-flex-grow="228" height="298" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-handwritten-function/customfunc_108.png" width="680"&gt;&lt;/p&gt;
&lt;h3 id="quick-look-at-if"&gt;Quick Look at If
&lt;/h3&gt;&lt;p&gt;One of the most used constructs in programming in any language must be the if statement. In order to help you build quick easy functions it is worth noting the syntax.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-xml" data-lang="xml"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;if [TEST] then [TRUE RESULT] else [FALSE RESULT]
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;So here is a very simple example of a function that calculates a delivery cost based on if the value is over 100 the delivery is free.&lt;/p&gt;
&lt;p&gt;&lt;img alt="simple if statement in an if function\n" class="gallery-image" data-flex-basis="735px" data-flex-grow="306" height="222" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-handwritten-function/customfunc_109.png" width="680"&gt;&lt;/p&gt;
&lt;h3 id="optional-parameters"&gt;Optional Parameters
&lt;/h3&gt;&lt;p&gt;Last topic in this post is to make parameter optional and handling when it is not provided. Optional parameters must be the last parameters defined in your function.&lt;/p&gt;
&lt;p&gt;So in my example I’m going to add a column that calculates tax based on the value and the optional tax rate. If no tax rate is given 20% is assumed.&lt;/p&gt;
&lt;p&gt;The parameters have to be withoutTax value followed by taxRate as the taxRate is optional. We can test for no taxRate being given by testing for taxRate = null. So our function is&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-xml" data-lang="xml"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;= (withoutTax as number, optional taxRate as number) as number =&amp;gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; if taxRate = null then 
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; withoutTax * 0.1 
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; else 
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; withoutTax * taxRate
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;&lt;img alt="picture of function definition" class="gallery-image" data-flex-basis="514px" data-flex-grow="214" height="317" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-handwritten-function/customfunc_110.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;Adding a tax column to example data shows how Alex’s tax rate is null so the tax is calculated at 10% and Jo’s tax is calculated at the given rate of 20%.&lt;/p&gt;
&lt;p&gt;&lt;img alt="data showing tax calculation" class="gallery-image" data-flex-basis="868px" data-flex-grow="361" height="188" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-handwritten-function/customfunc_111.png" width="680"&gt;&lt;/p&gt;
&lt;h3 id="conclusion-on-handwritten-functions"&gt;Conclusion on Handwritten Functions
&lt;/h3&gt;&lt;p&gt;These are very simple examples of functions but even these can save hours of repeating calculations. When your data has the same cleanup calculations being done to your data over many queries this allows for one function to be used repeatably.&lt;/p&gt;
&lt;h3 id="resources"&gt;Resources
&lt;/h3&gt;&lt;p&gt;I am not the first, and hopefully not the last to write blog posts on writing functions in M for Power Query. Here are a list of the resources I found useful. (If you know of any good ones I’ve missed please let me know!)&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a class="link" href="https://blog.crossjoin.co.uk/2016/05/15/creating-m-functions-from-parameterised-queries-in-power-bi/" target="_blank" rel="noopener"
 &gt;Chris Webb’s Creating M Functions From Parameterised Queries In Power BI&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://skillsmatter.com/skillscasts/10210-working-with-parameters-and-functions-in-power-query-excel-and-power-bi" target="_blank" rel="noopener"
 &gt;Chris Webb presenting at Skills Matter on Working with Parameters and Functions in Power Query/Excel and Power BI&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://ssbi-blog.de/writing-documentation-for-custom-m-functions/" target="_blank" rel="noopener"
 &gt;Lars Schreiber’s Writing documentation for custom M-functions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://bengribaudo.com/blog/2017/11/17/4107/power-query-m-primer-part1-introduction-simple-expressions-let" target="_blank" rel="noopener"
 &gt;Ben Gribaudo’s Power Query M Primer&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Power Query in Flow – Summarising the Data</title><link>https://hatfullofdata.blog/power-query-in-flow-summarising-the-data/</link><pubDate>Mon, 23 Sep 2019 08:33:01 +0000</pubDate><guid>https://hatfullofdata.blog/power-query-in-flow-summarising-the-data/</guid><description>&lt;p&gt;This is the third post in my series regarding Power Query in Flow. In this post I will look at the different options we have for summarising the data using grouping and pivoting with Power Query.&lt;/p&gt;
&lt;p&gt;Here is a list of all the posts in the series so far.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-in-microsoft-flow-1/" target="_blank" rel="noopener"
 &gt;Introducing Power Query in Microsoft Flow&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-in-microsoft-flow-2/" target="_blank" rel="noopener"
 &gt;Joining tables of data in Flow’s Power Query&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-in-flow-3/" target="_blank" rel="noopener"
 &gt;Summarising Data in Flow’s Power Query&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="introduction"&gt;Introduction
&lt;/h3&gt;&lt;p&gt;Again this post might be redundant if you’ve followed my advice and found out your dba’s favourite biscuits and got them to write you some awesome views in the database.&lt;/p&gt;
&lt;p&gt;For those that haven’t got access to the dba lets look at how we can summarise the data. We will look at two different types of summarising the data, firstly grouping and then pivoting. I have assumed you’ve got your data into a simple table using merges etc.&lt;/p&gt;
&lt;h3 id="using-grouping"&gt;Using Grouping
&lt;/h3&gt;&lt;p&gt;In this example we will group by a column and include some summary values. We will group by Product and then summary values of count of orders, total quantity and total value.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Initial data" class="gallery-image" data-flex-basis="279px" data-flex-grow="116" height="584" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-in-flow-summarising-the-data/Flow-PQ_201.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;This is how the data starts.&lt;/p&gt;
&lt;p&gt;On the toolbar click Transform Table and select Group By. We will start with a basic grouping of just one column to group by and one summary column of Order Count.&lt;/p&gt;
&lt;p&gt;I selected ProdName for the group by , entered a name for the summary column to give Order Count by counting the rows.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Basic Group by dialog" class="gallery-image" data-flex-basis="528px" data-flex-grow="220" height="309" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-in-flow-summarising-the-data/Flow-PQ_202.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt="Simple group" class="gallery-image" data-flex-basis="352px" data-flex-grow="146" height="353" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-in-flow-summarising-the-data/Flow-PQ_203.png" width="518"&gt;&lt;/p&gt;
&lt;p&gt;To add more columns to the summary I click on the cog wheel on the Grouped rows step to re-open the group by dialog.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Group by dialog" class="gallery-image" data-flex-basis="367px" data-flex-grow="153" height="444" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-in-flow-summarising-the-data/Flow-PQ_204.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;I click on Advanced to open up options to add extra grouping and aggregation options. I click on Add aggregation to add extra columns and change Operation to Sum and Column to total up Qty and Value. Clicking OK updates the table.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Data grouped" class="gallery-image" data-flex-basis="626px" data-flex-grow="261" height="260" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-in-flow-summarising-the-data/Flow-PQ_205.png" width="679"&gt;&lt;/p&gt;
&lt;h3 id="using-pivoting"&gt;Using Pivoting
&lt;/h3&gt;&lt;p&gt;Pivoting is a form of grouping that splits the data across a number of columns, for example total sales for each product across hours of the day. The first step is to reduce your table into just the data you need. So include the data for each row title, one set of data to be the column headings and one to be the data summarised.&lt;/p&gt;
&lt;p&gt;So I do Select Columns to reduce down to just Product, Hour of Sale and Qty of items sold.&lt;/p&gt;
&lt;p&gt;&lt;img alt="data ready to pivot" class="gallery-image" data-flex-basis="308px" data-flex-grow="128" height="482" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-in-flow-summarising-the-data/Flow-PQ_206.png" width="620"&gt;&lt;/p&gt;
&lt;p&gt;Next, select the column of data that will make the column headings in your pivot. In my example I select the SaleHour column. Then from Transform Column, select Pivot Column.&lt;/p&gt;
&lt;p&gt;&lt;img alt="pivot column dialog" class="gallery-image" data-flex-basis="480px" data-flex-grow="200" height="340" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-in-flow-summarising-the-data/Flow-PQ_207.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;In the Pivot column dialog, click on Advanced to reveal more options. In the Value column select the value you want to summarise and select the function you to use. Click OK to pivot the data.&lt;/p&gt;
&lt;p&gt;&lt;img alt="final pivot table after summarising the data" class="gallery-image" data-flex-basis="943px" data-flex-grow="393" height="173" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-in-flow-summarising-the-data/Flow-PQ_208.png" width="680"&gt;&lt;/p&gt;
&lt;h3 id="conclusion"&gt;Conclusion
&lt;/h3&gt;&lt;p&gt;Power Query provides a powerful way for summarising the data ready to process within the flow. As I stated earlier a view written by a dba would be more useful and quicker.&lt;/p&gt;</description></item><item><title>Joining Tables of Data in Flow’s Power Query</title><link>https://hatfullofdata.blog/power-query-in-microsoft-flow-joining-tables/</link><pubDate>Fri, 06 Sep 2019 20:00:01 +0000</pubDate><guid>https://hatfullofdata.blog/power-query-in-microsoft-flow-joining-tables/</guid><description>&lt;p&gt;This is the second post in my series regarding Power Query in Flow. In this post I will introduce one of the most powerful parts of Power Query, joining tables. Its the vlookup part of Power Query.&lt;/p&gt;
&lt;p&gt;Here is a list of all the posts in the series so far.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-in-microsoft-flow-1/" target="_blank" rel="noopener"
 &gt;Introducing Power Query in Microsoft Flow&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-in-microsoft-flow-2/" target="_blank" rel="noopener"
 &gt;Joining tables of data in Flow’s Power Query&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-in-flow-3/" target="_blank" rel="noopener"
 &gt;Summarising Data in Flow’s Power Query&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="introduction"&gt;Introduction
&lt;/h3&gt;&lt;p&gt;If you have made best buddy friends with your dba (database administrator) then this post is possibly not required because if your dba will write a view to do the join for you, that is always better. BTW in my experience, dbas like biscuits.&lt;/p&gt;
&lt;p&gt;For those who don’t have access to their dba or haven’t bought enough biscuits this what we are going to do. My database contains a table called productsales.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Table of product sales" class="gallery-image" data-flex-basis="593px" data-flex-grow="247" height="275" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-in-microsoft-flow-joining-tables/Flow-PQ_101.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;In the Product Sales table is a ProductID column. I am going to use this value to merge in the values from the Product table (i.e. joining tables) so I can get the name and price.&lt;/p&gt;
&lt;p&gt;&lt;img alt="products" class="gallery-image" data-flex-basis="564px" data-flex-grow="235" height="289" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-in-microsoft-flow-joining-tables/Flow-PQ_102.png" width="680"&gt;&lt;/p&gt;
&lt;h3 id="multiple-tables"&gt;Multiple Tables
&lt;/h3&gt;&lt;p&gt;We start by adding both tables to our query. If multiple tables are in a query, Power Query only returns one table. You can tell which table will be returned by looking at the list of tables. The tables with italic names will not be returned, therefore the un-italicised table will be returned.&lt;/p&gt;
&lt;p&gt;&lt;img alt="select query to load" class="gallery-image" data-flex-basis="699px" data-flex-grow="291" height="233" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-in-microsoft-flow-joining-tables/Flow-PQ_103.png" width="679"&gt;&lt;/p&gt;
&lt;p&gt;Right click on a table name to open the menu. Then select Enable Load to change the table that will be loaded. In this example I will select that productsales will be loaded.&lt;/p&gt;
&lt;h3 id="merging-tables"&gt;Merging tables
&lt;/h3&gt;&lt;p&gt;We want to add the product name and product price to product sales tables. We will do this by merging the two tables.&lt;/p&gt;
&lt;p&gt;Select the productsales table. Then from the toolbar, select Combine Tables and Merge Queries.&lt;/p&gt;
&lt;p&gt;&lt;img alt="merge queries" class="gallery-image" data-flex-basis="269px" data-flex-grow="112" height="360" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-in-microsoft-flow-joining-tables/Flow-PQ_105.png" width="404"&gt;&lt;/p&gt;
&lt;p&gt;In the Merge dialog that appears select table you wish to merge with in the drop down, in my example I select products table. Then click on the columns that match, in my example I select product id in both tables.&lt;/p&gt;
&lt;p&gt;&lt;img alt="merge dialog box" class="gallery-image" data-flex-basis="208px" data-flex-grow="86" height="783" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-in-microsoft-flow-joining-tables/Flow-PQ_104-2.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;Leave the join kind as Left Outer as this will keep all the sales records and find matching products. Further details of join types can be found on Radacad’s site. &lt;a class="link" href="https://radacad.com/choose-the-right-merge-join-type-in-power-bi" target="_blank" rel="noopener"
 &gt;https://radacad.com/choose-the-right-merge-join-type-in-power-bi&lt;/a&gt;&lt;/p&gt;
&lt;h3 id="expanding-the-table-column"&gt;Expanding the table column
&lt;/h3&gt;&lt;p&gt;Clicking OK will update the table to include a column which contains Tables.&lt;/p&gt;
&lt;p&gt;&lt;img alt="expand table column" class="gallery-image" data-flex-basis="470px" data-flex-grow="195" height="347" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-in-microsoft-flow-joining-tables/Flow-PQ_106.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;Click on the icon in top right of the new column to display the columns available in the tables. Unselect any un-required columns. Unselect the Use original column name as prefix to prevent the new columns being named long names for example products.ProdName. Then click OK to add the new columns.&lt;/p&gt;
&lt;p&gt;&lt;img alt="final data after joining tables" class="gallery-image" data-flex-basis="553px" data-flex-grow="230" height="295" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-in-microsoft-flow-joining-tables/Flow-PQ_107.png" width="680"&gt;&lt;/p&gt;
&lt;h3 id="conclusion-to-joining-tables"&gt;Conclusion to Joining Tables
&lt;/h3&gt;&lt;p&gt;Merging is a powerful addition to transforming data. It is better if a view can added to the database by your dba as that will always be more efficient. Power Query merges do offer a great alternative.&lt;/p&gt;</description></item><item><title>Power Query in Microsoft Flow – An Introduction</title><link>https://hatfullofdata.blog/power-query-in-microsoft-flow-1/</link><pubDate>Mon, 26 Aug 2019 21:34:42 +0000</pubDate><guid>https://hatfullofdata.blog/power-query-in-microsoft-flow-1/</guid><description>&lt;p&gt;This is the first post in a new series regarding Power Query in Microsoft Flow. In this post I will cover the basics of connecting to SQL Server and using Power Query to filter and sort the data and finally add calculated columns.&lt;/p&gt;
&lt;p&gt;This series assumes that you are not a user of Power Query desktop from Excel or Power BI.&lt;/p&gt;
&lt;p&gt;Here is a list of all the posts in the series so far.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-in-microsoft-flow-1/" target="_blank" rel="noopener"
 &gt;Introducing Power Query in Microsoft Flow&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-in-microsoft-flow-2/" target="_blank" rel="noopener"
 &gt;Joining tables of data in Flow’s Power Query&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-query-in-flow-3/" target="_blank" rel="noopener"
 &gt;Summarising Data in Flow’s Power Query&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="introduction-to-power-query-in-microsoft-flow"&gt;Introduction to Power Query in Microsoft Flow
&lt;/h3&gt;&lt;p&gt;In September 2018 Microsoft announced the integration of Power Query Online for Microsoft Flow. The reasons were to offer an alternative to OData and SQL for getting data from SQL Server connector. They included a hint that other data sources will come in the future but no details when or what.&lt;/p&gt;
&lt;p&gt;For those coming from Power Query for Excel or Power BI you need to understand you are currently limited to data stored in SQL Server online or server via a gateway. Also the Power Query Online environment is not as powerful as the desktop app version. It is still written in M, so you can do many of the transformations you can in Excel and Power BI’s Power Query.&lt;/p&gt;
&lt;h3 id="connecting-to-the-database"&gt;Connecting to the Database
&lt;/h3&gt;&lt;p&gt;Our first action is to connect Power Query to the data. Power Query action is from the SQL Server connector.&lt;/p&gt;
&lt;p&gt;&lt;img alt="sql server connection" class="gallery-image" data-flex-basis="462px" data-flex-grow="192" height="353" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-in-microsoft-flow-1/Flow-PQ_001.jpg" width="680"&gt;&lt;/p&gt;
&lt;p&gt;If this is the first time you’ve connected to the database you will be prompted for database details and the user id to use with a password.&lt;/p&gt;
&lt;p&gt;&lt;img alt="authentication and settings" class="gallery-image" data-flex-basis="292px" data-flex-grow="122" height="557" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-in-microsoft-flow-1/Flow-PQ_002.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;Then the action then changes to just show a button. This means you have connected to the database successfully.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Successful connection to power query in microsoft flow" class="gallery-image" data-flex-basis="1255px" data-flex-grow="523" height="130" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-in-microsoft-flow-1/Flow-PQ_004.png" width="680"&gt;&lt;/p&gt;
&lt;h3 id="selecting-data-to-transform"&gt;Selecting Data to Transform
&lt;/h3&gt;&lt;p&gt;When you click the Edit Query button for the first time, you will be presented with the tables and views in the database. You need to tick the tables and views that you need. For this post I’m just going select a single table Expenses and then click Next.&lt;/p&gt;
&lt;p&gt;&lt;img alt="select tables" class="gallery-image" data-flex-basis="457px" data-flex-grow="190" height="357" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-in-microsoft-flow-1/Flow-PQ_005.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;The next window is the main Online Power Query window, where we will add steps to transform your data.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;List of parameters and tables&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;View of the data at the current step&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;List of steps&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Toolbar used to add new steps&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="power query window" class="gallery-image" data-flex-basis="455px" data-flex-grow="189" height="358" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-in-microsoft-flow-1/Flow-PQ_006.png" width="680"&gt;&lt;/p&gt;
&lt;h3 id="filtering-data"&gt;Filtering Data
&lt;/h3&gt;&lt;p&gt;One of the most common requests on data is to filter the values. Clicking on the arrow next to column header will show you a list of values with tick boxes. Un-tick the unrequired values to filter your data. When you do this it will add a new step to the applied steps.&lt;/p&gt;
&lt;p&gt;&lt;img alt="filtering" class="gallery-image" data-flex-basis="439px" data-flex-grow="183" height="371" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-in-microsoft-flow-1/Flow-PQ_007.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;When you want to change the filtered value, you need to click the cog wheel next to the step. You can click on X to remove any step.&lt;/p&gt;
&lt;h3 id="sorting-data"&gt;Sorting Data
&lt;/h3&gt;&lt;p&gt;When you click the drop down arrow next the column heading, you can select Sort ascending or Sort descending. The first time you select sort, the data gets sorted and a new step is added to the applied steps.&lt;/p&gt;
&lt;p&gt;If you then sort another column, the existing step is just extended to add the next column to the sort. This is similar to adding a new sort level in Excel.&lt;/p&gt;
&lt;p&gt;&lt;img alt="sorting" class="gallery-image" data-flex-basis="1337px" data-flex-grow="557" height="122" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-in-microsoft-flow-1/Flow-PQ_008.png" width="680"&gt;&lt;/p&gt;
&lt;h3 id="adding-columns"&gt;Adding Columns
&lt;/h3&gt;&lt;p&gt;One of the most powerful parts of Power Query is to add columns using calculations. The interface can be really clever and also limited and drives me nuts which is perhaps a blog post all of its own.&lt;/p&gt;
&lt;p&gt;If you select a column and then click Add Column from the top ribbon you will get offered choices based on the column type. For example, if you have selected a number column you get offered Standard, Scientific and Trigonometry functions and rounding plus others.&lt;/p&gt;
&lt;p&gt;&lt;img alt="adding column" class="gallery-image" data-flex-basis="454px" data-flex-grow="189" height="359" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-in-microsoft-flow-1/Flow-PQ_009.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;The result of the calculation is placed in a new column.&lt;/p&gt;
&lt;p&gt;&lt;img alt="column added" class="gallery-image" data-flex-basis="676px" data-flex-grow="281" height="241" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-in-microsoft-flow-1/Flow-PQ_010.png" width="679"&gt;&lt;/p&gt;
&lt;p&gt;When you select multiple columns before selecting Add column, calculations to combine the columns are offered, such as difference between dates.&lt;/p&gt;
&lt;h3 id="renaming-and-selecting-columns"&gt;Renaming and Selecting Columns
&lt;/h3&gt;&lt;p&gt;You can rename any column by double clicking on the column header and typing in a new name. The first column you rename will add a new step to the query. If you rename another column straight after the Renamed Columns step is extended.&lt;/p&gt;
&lt;p&gt;It is always a good idea to only return the columns that are required by the Flow. Under Manage Columns you can select Choose columns. When the dialog is selected, you can tick or un-tick any column.&lt;/p&gt;
&lt;p&gt;&lt;img alt="choosing columns" class="gallery-image" data-flex-basis="283px" data-flex-grow="118" height="576" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-query-in-microsoft-flow-1/Flow-PQ_011.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;The choose columns step has a cog wheel so can be modified at any point. I recommend you use this step rather than delete columns as that cannot be tweaked using the cog wheel.&lt;/p&gt;
&lt;h3 id="finishing"&gt;Finishing
&lt;/h3&gt;&lt;p&gt;When you have finished transforming your data you can click Create on a new query or Update if you are editing an existing query. You can now use the data returned just as you would for getting items from any source.&lt;/p&gt;
&lt;h3 id="resources-for-power-query-in-microsoft-flow"&gt;Resources for Power Query in Microsoft Flow
&lt;/h3&gt;&lt;p&gt;As always there are a selection of resources out on the web.&lt;/p&gt;
&lt;p&gt;•Microsoft &lt;a class="link" href="https://flow.microsoft.com/en-us/blog/powerquery-flow/" target="_blank" rel="noopener"
 &gt;https://flow.microsoft.com/en-us/blog/powerquery-flow/&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Chris Webb&lt;a class="link" href="https://blog.crossjoin.co.uk/2018/09/26/using-power-query-and-microsoft-flow-to-automate-the-creation-of-csv-files/" target="_blank" rel="noopener"
 &gt;https://blog.crossjoin.co.uk/2018/09/26/using-power-query-and-microsoft-flow-to-automate-the-creation-of-csv-files/&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Erik Svensen&lt;a class="link" href="https://eriksvensen.wordpress.com/2018/09/25/powerquery-everywhere-now-in-microsoftflow-as-well/" target="_blank" rel="noopener"
 &gt;https://eriksvensen.wordpress.com/2018/09/25/powerquery-everywhere-now-in-microsoftflow-as-well/&lt;/a&gt;&lt;/p&gt;
&lt;h3 id="conclusion"&gt;Conclusion
&lt;/h3&gt;&lt;p&gt;Power Query is a great addition to the Flow tool set. I am really hoping for more data sources to be allowed and for the online Power Query to include more features from the desktop version.&lt;/p&gt;
&lt;p&gt;This is just an introduction so I will include best practices and further features.&lt;/p&gt;</description></item></channel></rss>