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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Anonymous
Not applicable

Dynamic unpivoting with DAX

I have a table, which for reasons, has to be unpivoted in PowerBI using DAX. At the moment, I'm using a simple, standard solution to accomplish this task using the standard SELECTCOLUMNS/UNION combo, something like:

 

 

 Table =
     VAR t1 =
     SELECTCOLUMNS ( 'Table', "Date", [Date], "Store", "Store1", "Value", [Store1] )
     VAR t2 =
     SELECTCOLUMNS ( 'Table', "Date", [Date], "Store", "Store2", "Value", [Store2] )
     VAR t3 =
     SELECTCOLUMNS ( 'Table', "Date", [Date], "Store", "Store3", "Value", [Store3] )
     RETURN
     UNION ( t1, t2, t3 )

 

 

It works but it's rather ugly and since I have to unpivot a lot of columns it's rather long and unwieldly. I would prefer a more dynamic solution where I could specify the columns to be unpivoted in a table, or in a variable but can't quite work it out.

 

So far I've come up with the following solution but it still involves hardcoding column names in the measure:

 

UnpivotExample =
     var Fields =
     VALUES(FieldList[FieldName]) //Table listing columns to be unpivoted

     var tbl =
     CROSSJOIN(
        SUMMARIZE(
           Table,
           Table[ID]),
           Fields)
     return

     ADDCOLUMNS(
        tbl,
        "Date",SWITCH(
           [FieldName],
           "col1", CALCULATE(FIRSTNONBLANK(Table[col1],1),FILTER(Table,Table[ID]=[ID])),
           "col2", CALCULATE(FIRSTNONBLANK(Table[col2],1),FILTER(Table,Table[ID]=[ID]))... etc

 

 

Is there a way to make it more dynamic, without hardcoding the column names themselves in the measure? (yes, I know it can be easily done in M but it's a DAX question).

Cheers,

Michal

 

2 REPLIES 2
_fm
Regular Visitor

Hi, 

Your example is very helpful but this won't work when you are adding measure inside your selectcolumn function. As I have to create a table with some attribute and values using table A, but it contains measures which I required. 

parry2k
Super User
Super User

@Anonymous why you cannot use Power Query for the same, why not use the relative tool for unpivot rather than doing it in DAX?



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors