Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
arimoldi
Resolver I
Resolver I

Transpose & Pivot using Power Query

Hi,

 

I have an input dataset in this format:

 

ACTIVITY / DATE01/10/202402/10/202403/10/202404/10/202405/10/2024
A8 41 
B 3 15
C 3 1 
D 2 1 
E  4  
F   43

 

Is there any way to obtain a dataset like the following using the Power Query Editor or in any other way?

 

DATEACTIVITYNUM
01/10/2024A8
01/10/2024B 
01/10/2024C 
01/10/2024D 
01/10/2024E 
01/10/2024F 
02/10/2024A 
02/10/2024B3
02/10/2024C3
02/10/2024D2
02/10/2024E 
02/10/2024F 
03/10/2024A4
03/10/2024B 
03/10/2024C 
03/10/2024D 
03/10/2024E4
03/10/2024F 
04/10/2024A1
04/10/2024B1
04/10/2024C1
04/10/2024D1
04/10/2024E 
04/10/2024F4
05/10/2024A 
05/10/2024B5
05/10/2024C 
05/10/2024D 
05/10/2024E 
05/10/2024F3

 

Thanks,

Andrea

1 ACCEPTED SOLUTION

Hi @arimoldi 

 

Excel

vnuocmsft_2-1730946785853.png

 

I retested your problem by first importing the excel table into the power bi desktop and going to the power query interface to get the following table:

 

vnuocmsft_1-1730946758438.png

 

1. If you don't want empty values to display data, consider replacing "null" with Spaces.

 

In the same step, you just need to change the "0" to " ".

 

vnuocmsft_0-1730946459195.png

 

vnuocmsft_3-1730946975403.png

 

Use the first line as the title.

 

vnuocmsft_4-1730947040437.png

Delete "Changed Type1" from the step bar on the right.

 

vnuocmsft_7-1730947336503.png

 

2. Select the first column and click Fill -> Down. The first column will be filled in automatically.

 

vnuocmsft_5-1730947153730.png

 

Finally, the column with the date header is also selected for unpivot.

 

vnuocmsft_8-1730947503424.png

 

Change the column name as required.

 

vnuocmsft_9-1730947587479.png

 

Regards,

Nono Chen

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

 

 

 

 

View solution in original post

13 REPLIES 13
arimoldi
Resolver I
Resolver I

Thank you all for the answers,

 

I used the "Unpivot all other columns" functionality as you suggested to create the desired dataset.

 

I have 2 more questions:

 

1. if I don't insert a value (eg 0) in the column it won't be unpivoted by Power Query; is there any smart way to manage this? Or do I have to manually insert 0 in all the blank cells?

2. column "ACTIVITY" is associated to another column "MACRO-ACTIVITY" but it is a merged column, so when I use the Unpivot functionality not all the "ACTIVITY" are associated to the related "MACRO-ACTIVITY"; is there any way to do this?

 

arimoldi_0-1730822888736.png


Thanks,

Andrea

 

Hi @arimoldi 

 

First of all, thank you very much for your prompt reply!

 

It seems that you already know the solution to this problem. In response to your two questions:

 

1. You can use "Replace Values" to replace empty values.

 

vnuocmsft_0-1730874426885.png

 

2. Assuming that your two tables have the same ACTIVITY column, merge the two tables based on the ACTIVITY column in the power query. And expand.

 

vnuocmsft_1-1730874615639.png

 

vnuocmsft_2-1730874781361.png

 

vnuocmsft_3-1730874796174.png

 

You can get a table like this.

 

vnuocmsft_4-1730874847259.png

 

After replacing the null value, select all date titled columns to try unpivot.

 

vnuocmsft_5-1730875146101.png

 

Regards,

Nono Chen

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

 

 

 

 

Hi,

 

thanks for the replay.

 

For point #1 ok, I was wondering if there was another way to manage null instead of replacing them with 0, but anyway it is fine.

 

For point #2 your solution seems to be applicable only in the case I have a domain table where alla ACTIVITY and MACRO_ACTIVITY are listed, but this is not the case... I have just one table and the MACRO_ACTIVITY are merged cells so when I import the dataset not all the ACTIVITY are associated to the related MACRO_ACTIVITY.

 

Thanks,

Andrea

 

Hi @arimoldi 

 

Excel

vnuocmsft_2-1730946785853.png

 

I retested your problem by first importing the excel table into the power bi desktop and going to the power query interface to get the following table:

 

vnuocmsft_1-1730946758438.png

 

1. If you don't want empty values to display data, consider replacing "null" with Spaces.

 

In the same step, you just need to change the "0" to " ".

 

vnuocmsft_0-1730946459195.png

 

vnuocmsft_3-1730946975403.png

 

Use the first line as the title.

 

vnuocmsft_4-1730947040437.png

Delete "Changed Type1" from the step bar on the right.

 

vnuocmsft_7-1730947336503.png

 

2. Select the first column and click Fill -> Down. The first column will be filled in automatically.

 

vnuocmsft_5-1730947153730.png

 

Finally, the column with the date header is also selected for unpivot.

 

vnuocmsft_8-1730947503424.png

 

Change the column name as required.

 

vnuocmsft_9-1730947587479.png

 

Regards,

Nono Chen

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

 

 

 

 

Handling Blanks in the Unpivoted Column

Power Query can indeed leave blanks as they are during unpivoting, which can be a bit tricky if you need to replace them with zeroes.

To handle this, you can use the Replace Values function after unpivoting to automatically fill in blank cells with 0:

  • In Power Query, after unpivoting, select the NUM column (or whichever column now contains the unpivoted values).
  • Go to Transform > Replace Values.
  • In the Replace Values dialog, leave the Value to Find field blank (this will match blank cells) and set the Replace With field to 0.
  • Click OK. All blanks in this column will now be replaced with 0.

Alternatively, if you'd like to handle this step during unpivoting itself, you can first use Fill Down (if blank values indicate the same previous value) or Replace Blanks with 0 on the original table before unpivoting.

BITomS
Responsive Resident
Responsive Resident

@123abc @arimoldi I think there may be some confusion here - the blanks are only brought through within the unpivoting if the data type of the original date columns is text. If the data type is a number, any null values do not generate an associated row after unpivoting. I don't think the data type is specified in this thread, so it depends on this.

 

That said, converting the data type of the columns (if the type is currently number, which is what I assumed) to text is another option and then doing the unpivoting as per what @123abc outlined.

 

BITomS
Responsive Resident
Responsive Resident

@arimoldi , it depends if you really need the date-activity combinations with no values? You don't have to enter the zeros manually - there is another option in Power Query to 'replace values', so you could create steps to replace the blank values with a 0 for each column.

 

BITomS_0-1730823704421.png

 

If there is another column, you can select both columns in Power Query (CTRL + Select) and then use the 'Unpivot Other Columns' functionality.

hi @arimoldi ,

 

1. you can insert a 0 and replace the 0 with "" afterwards

 

2. try select both macro activity and activity columns, and unpivot other columns.

Hi,

 

thanks for the reply.

 

For point #1 ok, thanks.

 

For point #2 I tried, but since MACRO_ACTIVITY is composed by merged cells when I unpivot the tablemost of the MACRO_ACTIVITY remain null.

 

Thanks,

Andrea

 

Sahir_Maharaj
Super User
Super User

Hello @arimoldi,

 

Can you please try this approach:

UnpivotedData = 
VAR DatesList = {"01/10/2024", "02/10/2024", "03/10/2024", "04/10/2024", "05/10/2024"}
RETURN
UNION(
    SELECTCOLUMNS(
        FILTER('OriginalData', TRUE),
        "DATE", DATE(2024, 10, 1),
        "ACTIVITY", 'OriginalData'[ACTIVITY],
        "NUM", 'OriginalData'[01/10/2024]
    ),
    SELECTCOLUMNS(
        FILTER('OriginalData', TRUE),
        "DATE", DATE(2024, 10, 2),
        "ACTIVITY", 'OriginalData'[ACTIVITY],
        "NUM", 'OriginalData'[02/10/2024]
    ),
    SELECTCOLUMNS(
        FILTER('OriginalData', TRUE),
        "DATE", DATE(2024, 10, 3),
        "ACTIVITY", 'OriginalData'[ACTIVITY],
        "NUM", 'OriginalData'[03/10/2024]
    ),
    SELECTCOLUMNS(
        FILTER('OriginalData', TRUE),
        "DATE", DATE(2024, 10, 4),
        "ACTIVITY", 'OriginalData'[ACTIVITY],
        "NUM", 'OriginalData'[04/10/2024]
    ),
    SELECTCOLUMNS(
        FILTER('OriginalData', TRUE),
        "DATE", DATE(2024, 10, 5),
        "ACTIVITY", 'OriginalData'[ACTIVITY],
        "NUM", 'OriginalData'[05/10/2024]
    )
)

Did I answer your question? Mark my post as a solution, this will help others!

If my response(s) assisted you in any way, don't forget to drop me a "Kudos" 🙂

Kind Regards,
Sahir Maharaj
Data Scientist | Data Engineer | Data Analyst | AI Engineer
P.S. Want me to build your Power BI solution? (Yes, its FREE!)
➤ Lets connect on LinkedIn: Join my network of 15K+ professionals
➤ Join my free newsletter: Data Driven: From 0 to 100
➤ Website: https://sahirmaharaj.com
➤ Email: sahir@sahirmaharaj.com
➤ Want me to build your Power BI solution? Lets chat about how I can assist!
➤ Join my Medium community of 30K readers! Sharing my knowledge about data science and artificial intelligence
➤ Explore my latest project (350K+ views): Wordlit.net
➤ 100+ FREE Power BI Themes: Download Now
LinkedIn Top Voice in Artificial Intelligence, Data Science and Machine Learning
BITomS
Responsive Resident
Responsive Resident

Hi @arimoldi ,

Yes, if you right click on the Activity column in Power Query and select 'Unpivot Other columns', this should achieve what you need.

If you 100% need to show the dates with empty values, I'd suggest populating the blanks as zeros before doing the unpivot. Not being able to see your pbix model, I don't know the context in which you are using this, but assuming a FACT table, the empty values not appearing after the unpivot shouldn't matter if you have a date dimension table your end users are filtering with in your end report.

 

Hope that helps!

123abc
Super User
Super User

Yes you can do this with the help of Pivot and Unpivot data in power query.

 

1.Go to Power BI Power Query Editor.

2.Then go to Transform Option.

3. Under Any Column Group there are many opitons but two are for you a: Unpivot Columns, b: Pivot Coumn.

4. Unpitovt Columns will solve your issue.

 

Please follow below detail:

  1. Load the Data into Power Query:

    • Load your data into Power Query Editor by selecting your data range and choosing "Data" > "From Table/Range" in Excel (make sure your data is in a table format).
  2. Unpivot the Data:

    • In Power Query Editor, select the date columns (e.g., 01/10/2024, 02/10/2024, etc.).
    • Go to the Transform tab and select Unpivot Columns. This will create three columns: ACTIVITY, Attribute, and Value.
  3. Rename Columns:

    • Rename the Attribute column to DATE.
    • Rename the Value column to NUM.
  4. Set Data Types:

    • Ensure that the DATE column is set to the Date data type, and the NUM column is set to the Whole Number data type (or Decimal, depending on your data).
  5. Sort (Optional):

    • You may want to sort the table by DATE and then by ACTIVITY to get the desired order.
  6. Close & Load:

    • Click Close & Load to load the transformed data back into Excel or Power BI.

Result

You should now have your data in the desired format:

DATE ACTIVITY NUM

01/10/2024A8
01/10/2024B 
01/10/2024C 
01/10/2024D 
01/10/2024E 
01/10/2024F 
02/10/2024A 
02/10/2024B3
.........

This unpivoting technique transforms your wide format into the desired long format with columns for DATE, ACTIVITY, and NUM.

FreemanZ
Super User
Super User

hi @arimoldi ,

 

select the ACTIVITY column and unpivot other columns.

 

more about unpivot:

https://support.microsoft.com/en-us/office/unpivot-columns-power-query-0f7bad4b-9ea1-49c1-9d95-f5882....

 

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.