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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
axiosfabric
New Member

Dynamic column generation using DAX

PROFSUMMARY =
SUMMARIZE (
Hi,
Training,
Training[ID],
Training[Prof],
"DP101", CALCULATE ( SUM ( Training[Att Hrs] ), Training[Code] = "DP101",Training[Mode] = "Virtual" ),
"DP103", CALCULATE ( SUM ( Training[Att Hrs] ), Training[Code] = "DP103",Training[Mode] = "Virtual" ),
"DP102", CALCULATE ( SUM ( Training[Att Hrs] ), Training[Code] = "DP102",Training[Mode] = "Virtual" ),
"DP320", CALCULATE ( SUM ( Training[Att Hrs] ), Training[Code] = "DP320",Training[Mode] = "Virtual" ),
)
How can be this red section generate dynamically based on the available distinct code in Training[Code].
can someone help me to achive this??

6 REPLIES 6
Rakesh1705
Super User
Super User

Hi @axiosfabric 
Source data

Rakesh1705_0-1722241220446.png

Taking a Matrix Visualization and turning off column/row subtotals

Rakesh1705_1-1722241292189.png

 

if this solves your problem then please accept the same as your solution.

Anonymous
Not applicable

Hi @axiosfabric 

 

Maybe you can try to use the Pivot Column feature in the power query:

Open the Power query, select the code column and click the pivot column in the transform pane:

vzhengdxumsft_0-1722221758220.png

Then Select Hrs in the Value Column box and select Sum in Aggregaet Value Function box:

vzhengdxumsft_1-1722221856888.png

Click Close & Apply:

vzhengdxumsft_2-1722221984216.png

The result is as follow:

vzhengdxumsft_3-1722221998076.png

 

 

Best Regards

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

Gaga_Jin
Frequent Visitor

1. Create a separate table for distinct codes:
DistinctCodes = DISTINCT(Training[Code])
2. Create a calculated table to summarize the data dynamically:

PROFSUMMARY =
VAR DistinctCodes = DISTINCT(Training[Code])
RETURN
ADDCOLUMNS(
SUMMARIZE(
Training,
Training[ID],
Training[Prof]
),
"DynamicColumns",
SELECTCOLUMNS(
DistinctCodes,
"Code", [Code],
"TotalHours",
CALCULATE(
SUM(Training[Att Hrs]),
Training[Code] = [Code],
Training[Mode] = "Virtual"
)
)
)

 

@Gaga_Jin There are an array of issues with that formula. First, [Code] will not be recognized 5 lines up from the bottom. The reason is that you are trying to refer to a column that you are adding while you are adding it which won't work. You would need 2 ADDCOLUMNS if you wanted to reference [Code]. Second, you will get an error stating that "Multiple columns cannot be convereted to a scalar" or something along those lines because you are trying to fit 2 columns of data into a single column. Third, even if none of that was a problem ( and it is ), you would still not arrive at the desired table, you would have a column called Code and a column called TotalHours which isn't what is desired.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...
Greg_Deckler
Community Champion
Community Champion

@axiosfabric Not really understanding this at all. Can you post sample data and expected output? 

Sorry, having trouble following, can you post sample data as text and expected output?
Not really enough information to go on, please first check if your issue is a common issue listed here: https://community.powerbi.com/t5/Community-Blog/Before-You-Post-Read-This/ba-p/1116882

Also, please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490

The most important parts are:
1. Sample data as text, use the table tool in the editing bar
2. Expected output from sample data
3. Explanation in words of how to get from 1. to 2.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Training =

ProfCodehrs
User 1DP1012
User 2DP1032
User 3DP1022
User 4DP3202
User 1DP3202


Expected table result

ProfDP101DP103DP102DP320
User 12  2
User 2 2  
User 3  2 
User 4   2


based on this below code I can achive the above format 
PROFSUMMARY =
SUMMARIZE (
Training,
Training[ID],
Training[Prof],
"DP101", CALCULATE ( SUM ( Training[Att Hrs] ), Training[Code] = "DP101",Training[Mode] = "Virtual" ),
"DP103", CALCULATE ( SUM ( Training[Att Hrs] ), Training[Code] = "DP103",Training[Mode] = "Virtual" ),
"DP102", CALCULATE ( SUM ( Training[Att Hrs] ), Training[Code] = "DP102",Training[Mode] = "Virtual" ),
"DP320", CALCULATE ( SUM ( Training[Att Hrs] ), Training[Code] = "DP320",Training[Mode] = "Virtual" ),
)

but here I need to mention the "DP101" as hard coded

"DP101", CALCULATE ( SUM ( Training[Att Hrs] ), Training[Code] = "DP101",Training[Mode] = "Virtual" ),
I need to generate this section dynamically based on the the distinct code availble 

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

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

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.