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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
GabinAM
Frequent Visitor

Add Rows - Loop

Hello everyone! 😊

Hope you're doing well! 

Initial Situation

I'm currently working on tricky DB, please see below an example, wtih some informations stored in columns (ID/Date/Underlying) and some others vertically (Label/Level) (it's a bit hard to express clearly). 

IDLabelLevelDateUnderlying
1Accumulation30001/01/2024Potato
1Barrier40001/01/2024Potato
2Up30003/01/2024Green Beans
2Down40003/01/2024Green Beans
2Middle45003/01/2024Green Beans
3Up30005/01/2024Carrots
3Down40005/01/2024Carrots
3Middle35005/01/2024Carrots
3Barrier50005/01/2024Carrots

 

Expected Result

I would like to display a table or matrix in PowerBI (based on a ID filter) as folow: 

Example 1 

IDAccumulationBarrierDateUnderlying
130040001/01/2024Potato


Example 2 

IDUpDownMiddleDateUnderlying
230040045003/01/2024Green Beans


Example 3

IDUpDownMiddleBarrierDateUnderlying
330040035014/05/190105/01/2024Carrots

 

However...

So far, I'm a bit stuck as I cannot reach this result either with a Table or with a Matrix. Please see below an example: 

GabinAM_0-1715258528007.png


*******************************************

In order to reach my result, I was thinking about creating a Looping Querry adding for each ID two additional rows as follow : 

IDLabelLevelDateUnderlying
1Accumulation30001/01/2024Potato
1Barrier40001/01/2024Potato
1Date01/01/2024  
1UnderlyingPotato  
2Up30003/01/2024Green Beans
2Down40003/01/2024Green Beans
2Middle45003/01/2024Green Beans
2Date03/01/2024  
2UnderlyingGreen Beans  
3Up30005/01/2024Carrots
3Down40005/01/2024Carrots
3Middle45005/01/2024Carrots
3Barrier50005/01/2024Carrots
3Date05/01/2024  
3UnderlyingCarrots  


I don't know if it's managable on PowerQuerry or if there is a easier way to reach the expected result in PowerBI. Any help would be greatly appreaciated! 🙏

Please find here the PBIX and Excel used for the example. 

Many thanks in advance! 

1 ACCEPTED SOLUTION
dufoq3
Super User
Super User

Hi @GabinAM, Power Query solution:

 

Result (you can delete last step if you want to see separate tables)

dufoq3_0-1715260355997.png

 

let
    Source = Excel.Workbook(File.Contents("Z:\support\Execution\GAM\Control - Check - Data\20240408 - ADM New DB\Test 2\TEST.xlsx"), true, true),
    Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
    ChangedType = Table.TransformColumnTypes(Sheet1_Sheet,{{"ID", Int64.Type}, {"Label", type text}, {"Level", Int64.Type}, {"Date", type date}, {"Underlying", type text}}),
    GroupedRows = Table.Group(ChangedType, {"ID"}, {{"All", each Table.Pivot(_, List.Distinct([Label]), "Label", "Level"), type table}}),
    CombinedAll = Table.Combine(GroupedRows[All])
in
    CombinedAll

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

View solution in original post

6 REPLIES 6
dufoq3
Super User
Super User

Hi @GabinAM, Power Query solution:

 

Result (you can delete last step if you want to see separate tables)

dufoq3_0-1715260355997.png

 

let
    Source = Excel.Workbook(File.Contents("Z:\support\Execution\GAM\Control - Check - Data\20240408 - ADM New DB\Test 2\TEST.xlsx"), true, true),
    Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
    ChangedType = Table.TransformColumnTypes(Sheet1_Sheet,{{"ID", Int64.Type}, {"Label", type text}, {"Level", Int64.Type}, {"Date", type date}, {"Underlying", type text}}),
    GroupedRows = Table.Group(ChangedType, {"ID"}, {{"All", each Table.Pivot(_, List.Distinct([Label]), "Label", "Level"), type table}}),
    CombinedAll = Table.Combine(GroupedRows[All])
in
    CombinedAll

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Hello @dufoq3 it works wonderfully! 

I'm just checking how can we hide the column if the value is null for the relevant ID? 

Basicly, I have ~ 15/20 different ID with differents labels so it's not very handy to keep all of them on the table... 

If you want to achive this, remove my Power Query code and use the one you uploaded as an example. Then you can use matrix like this:

 

dufoq3_1-1715262410248.png

 

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Hi @dufoq3 just to follow on this topic, you can display only the columns for the selected iteams by selecting the columns in PowerQuerry and then Unpivot Only Selected Columns.

 

let
Source = Excel.Workbook(File.Contents("Z:\support\Execution\GAM\Control - Check - Data\20240408 - ADM New DB\Test 2\TEST.xlsx"), true, true),
Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
ChangedType = Table.TransformColumnTypes(Sheet1_Sheet,{{"ID", Int64.Type}, {"Label", type text}, {"Level", Int64.Type}, {"Date", type date}, {"Underlying", type text}}),
GroupedRows = Table.Group(ChangedType, {"ID"}, {{"All", each Table.Pivot(_, List.Distinct([Label]), "Label", "Level"), type table}}),
CombinedAll = Table.Combine(GroupedRows[All]),
#"Unpivoted Only Selected Columns" = Table.Unpivot(CombinedAll, {"Accumulation", "Barrier", "Up", "Down", "Middle", "Higher", "Lower", "Haut", "Bas"}, "Attribute", "Value")
in
#"Unpivoted Only Selected Columns"


Then in a Matrix. 

GabinAM_0-1715356818505.png

GabinAM_1-1715357011112.png


I guess you know this functionnality but just in case someone need it in the future!

Again many thanks for your help! 🙌

Many thanks @dufoq3 

Let's consider it solved! 👌

OK 🙂


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

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!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

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