Combining X Number of Rows in Power Query for Power BI, Excel and Power Query Online

Combining X Number of Rows in Power Query for Power BI, Excel and Power Query Online

A while back, I was working on a project involving getting data from Excel files. The Excel files contain the data in sheets and tables. Getting the data from the tables is easy. However, the data in the sheets have some commentaries on top of the sheet, then the column names and then the data itself. Something like below:

Sample data
Sample data

This approach is pretty consistent across many Excel files. The customer wants to have the commentary in the column names when the data is imported into Power BI. So the final result must look like this:

Sample Data to be loaded into Power BI
Sample Data to be loaded into Power BI

The business requirement though is to combine the first 3 rows of data and promote it as the column name.

The Challenge

Let’s connect the Excel file and look at the data in Power BI Desktop.

Connecting to sample data from Power BI Desktop
Connecting to sample data from Power BI Desktop

As you can see in the preceding image, Power BI, or more precisely, Power Query, sees the data in Table format. After we click the Transform Data button, this is what we get in Power Query Editor:

Connected to sample data from Power Query in Power BI Desktop
Connected to sample data from Power Query in Power BI Desktop

We all know that tables consist of Columns and Rows. The conjunction of a column and a row is a Cell. What we require to do is to concatenate the values of cells from the first three rows. We also have to use a Space character to separate the values of each cell from the others.

Column, rows and cells in a Table in Power BI
Column, rows and cells in a Table

In Power Query, we can get each row of data in as a Record with the following syntax:

Table{RecordIndex}

In the above syntax, the Table can be the results of the previous transformation step, and the RecordIndex starts from 0. So to get the first row of the table in the preceding image, we use the following syntax:

#"Changed Type"{0}

Where the #"Changed Type" is the previous step. Here are the results of running the preceding expression:

Getting the first row of a Table
Getting the first row of a Table

So we can get the second and third rows with similar expressions. The following image shows the entire codes in the Advanced Editor:

Power Query expressions in Advanced Editor in Power BI Desktop
Power Query expressions in Advanced Editor

But how do we concatenate the values of the rows?

Continue reading “Combining X Number of Rows in Power Query for Power BI, Excel and Power Query Online”

Power BI 101, What is Power BI

Many people talk about Power BI, its benefits and common challenges, and many more want to learn Power BI, which is excellent indeed. But there are many misconceptions and misunderstandings amongst the people who think they know Power BI. In my opinion, it is a significant risk in using tools without knowing them, and using the technology is no different. The situation is even worse when people who must know the technology well don’t know it, but they think they do. These people are potential risks to the businesses that want to adopt Power BI as their primary analytical solution across the organisation. As a part of my day-to-day job, I communicate with many people interacting with Power BI. Amongst many knowledgeable users are some of those who confuse things pretty frequently, which indicates a lack of understanding of the basic concepts.
So I decided to write a series of Power BI 101 to explain the basics of the technology that we all love in simple language. Regardless of your usage of Power BI, I endeavour to help you know what to expect from Power BI. This is the first part of this series.

What is Power BI?

I do not frequently get the “What is Power BI” question from my customers, my website’s comments, or my students within the training courses. It is indeed a question that I often ask people. I usually ask the question to indicate people’s level of understanding on different occasions, such as when a friend wants to know more about Power BI, or in a job interview from a candidate who applied for a Power BI related role, or my students attending a training course. Depending on the context that I ask the question, the responses are often pretty different.

It is the general rule of thumb to know what a “thing” is before using it. The “What is X?” (and X is the name of a “thing”) is a broad question, so the answer is also broad. Therefore we usually need more digging to get a better understanding of the “thing”. 

In our case, the “thing” is Power BI, so the question is “What is Power BI?”. And the answer is:

“Power BI is the Business Analytics platform part of a larger SaaS platform called Power Platform offering from Microsoft.”

Now, let’s dig a bit more with two more questions:

  • What is a Data Platform?
  • What is Saas?

Let’s quickly answer those questions.

Continue reading “Power BI 101, What is Power BI”

Quick Tips: Adding Leading Zero to Integer Values (Padding) with DAX and Power Query

Quick Tips: Adding Leading Zero to Integer Values (Padding) with DAX and Power Query

There are some cases that we want to add a leading zero to a digit, such as showing 01 instead of 1, 02 instead of 2 and so on. We have two options to do this in Power BI, doing it in Power Query or doing it with DAX.

Adding a Leading Zero in Power Query

The first method is doing it in Power Query using the Text.PadStart() function.

Here is how the syntax of the function:

Text.PadStart(text as nullable text, count as number, optional character as nullable text)

And here is how the function works:

Text.PadStart(input string, the length of the string, an optional character to be added to the beginning of the string util we reach to the string length)

For example, Text.PadStart("12345", 10 , "a") returns aaaaa12345 and Text.PadStart("1", 2 , "0") returns 01.

Let’s create a list of integer values between 1 to 20 with the following expression:

{1..20}
Creating a List of Integer Values Between 1 to 20 In Power Query
Creating a List of Integer Values Between 1 to 20 In Power Query

Now we convert the list to a table by clicking the To Table button from the Transform tab:

Converting a List to a Table in Power Query
Converting a List to a Table in Power Query

Now we add a new column by clicking the Custom Column from the Add Column tab from the ribbon bar:

Adding a New Column to a Table in Power Query
Adding a New Custom Column to a Table in Power Query

Now we use the following expression in the Custom Column window to pad the numbers with a leading zero:

Continue reading “Quick Tips: Adding Leading Zero to Integer Values (Padding) with DAX and Power Query”

Quick Tips: Enabling Modern Visual Styles on Old Reports in Power BI Desktop

Quick Tips: Enabling Modern Visual Styles on Old Reports in Power BI Desktop

Do you have an old report created in 2016? If you do, then you know that the visuals’ style was pretty different back then. If you don’t like to see all visuals in their old style where the headings overlap the other visuals and you don’t know how to switch the visuals to their modern style, then continue reading. The following screenshot is from a very old report I built for a Power BI demo in Feb 2016! Time flies mate!

Old Style Visuals in Power BI Desktop
Old Style Visuals in Power BI Desktop

If you are an old Power BI developer, you can clearly remember those days when we had many side-by-side slicers on the report canvas and sometimes it was hard to click a slicer because hovering over the visuals activates the visual frames and it is easy to mistakenly click a different visual.

The other issue with the old visual style is that the Visual Header options of the Visualisations pane from the Format tab are not available for traditional visuals.

Visual header options are not available for old style visual
Visual header options are not available for old-style visual

I see some developers don’t like the old visual style. It is interesting that when we work on an old report, even if we open the report in the latest version of Power BI Desktop, the visuals’ style remains the same and it doesn’t change if we add new visuals to a new page. Some developers go through a lot of pain to get the visuals to work in the modern style. I even know some developers copied all queries from the old file’s Power Query and pasted them to a new Power BI file. Then they used Tabular Editor to copy the tabular objects from the old report to the new one. And some even started to build everything from scratch. If you are one of those developers, be aware that there is a simple setting that can help to quickly switch all the old visuals to their new modern style.

Continue reading “Quick Tips: Enabling Modern Visual Styles on Old Reports in Power BI Desktop”