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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

Reply
Anonymous
Not applicable

How to avoid hard coding Dax

Hi I have a table as follows

CustIDGroupIDCustTypeAmountGroup_CustType_Concat
11Intermediate101Intermediate 
11Intermediate 201Intermediate 
23Regular303Regular
23Regular 403Regular
23Regular 303Regular
32Intermediate 202Intermediate 
32Intermediate 602Intermediate  
44Rare404Rare 
51Rare30

1Rare 

I have concatinated GroupID and CustType to form unique combination.

I have then created what-if parameters for each CustType grouped by Group IDs as seen below.

jtan20_0-1652856672241.png

 

The goal is to sum 'Amount' for each unique value in Group_CustType_Concat and then multiply it by the respective what-if parameter value. In the current Dax I am creating  measures for each unique concatination to calculate the sum of amount and then multiply it by the respective parameter value. Another measure is created to add the results of the measures and group them by CustType to form a line chart. 

 

1Intermediate= Calculate(sum('Table'[amount]),'Table'[Group_CustType_Concat]=="1Intermediate") * '1Intermediate'[1Intermediate Value]

 

This is repeated for all unique Group_CustType_Concat and then added together using switch statement based on each custType.

 

Linechart = var type=IF(ISBLANK(SELECTEDVALUE('Table'[CustType])),TRUE,SELECTEDVALUE('Table'[CustType]))
return switch(type,
"Intermediate", [1Intermediate] + [2Intermediate] + [3Intermediate]
,"Regular", [1Regular] + [2Regular]+ [3Regular]

 

However, this is a very inefficient way, as there can be multiple Groups and CustTypes leading to a large number of combinations. Is there a more efficient way of doing this, so I don't have to hardcode Dax for each Group_CustType_Concat? Maybe some kind of loop that itterates through each Group_CustType_Concat and automatically calculates the sum and multiplies it by it's respective parameter? Maybe power query loop?

1 ACCEPTED SOLUTION
v-jingzhang
Community Support
Community Support

Hi @Anonymous 

 

You can try this measure. But the section in SWITCH function is still hard coding. 

Result = 
VAR _table =
    SUMMARIZE (
        'Table',
        'Table'[GroupID],
        'Table'[CustType],
        'Table'[Group_CustType_Concat],
        "Sum_Of_Amount", SUM ( 'Table'[Amount] )
    )
VAR _table2 =
    ADDCOLUMNS (
        _table,
        "Amount*Parameter",
            [Sum_Of_Amount]
                * SWITCH (
                    [Group_CustType_Concat],
                    "1Intermediate", [Intermediate G1 Value],
                    "2Intermediate", [Intermediate G2 Value],
                    "1Regular", [Regular G1 Value],
                    "2Regular", [Regular G2 Value],
                    "1Rare", [Rare G1 Value],
                    "2Rare", [Rare G2 Value]
                )
    )
RETURN
    SUMX ( _table2, [Amount*Parameter] )

 

I use a table visual in my sample. For a line chart, you can put CustType on Axis or Legend to check the result. 

vjingzhang_0-1653287417593.png

 

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

 

View solution in original post

1 REPLY 1
v-jingzhang
Community Support
Community Support

Hi @Anonymous 

 

You can try this measure. But the section in SWITCH function is still hard coding. 

Result = 
VAR _table =
    SUMMARIZE (
        'Table',
        'Table'[GroupID],
        'Table'[CustType],
        'Table'[Group_CustType_Concat],
        "Sum_Of_Amount", SUM ( 'Table'[Amount] )
    )
VAR _table2 =
    ADDCOLUMNS (
        _table,
        "Amount*Parameter",
            [Sum_Of_Amount]
                * SWITCH (
                    [Group_CustType_Concat],
                    "1Intermediate", [Intermediate G1 Value],
                    "2Intermediate", [Intermediate G2 Value],
                    "1Regular", [Regular G1 Value],
                    "2Regular", [Regular G2 Value],
                    "1Rare", [Rare G1 Value],
                    "2Rare", [Rare G2 Value]
                )
    )
RETURN
    SUMX ( _table2, [Amount*Parameter] )

 

I use a table visual in my sample. For a line chart, you can put CustType on Axis or Legend to check the result. 

vjingzhang_0-1653287417593.png

 

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

 

Helpful resources

Announcements
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

February 2024 Update Carousel

Power BI Monthly Update - February 2024

Check out the February 2024 Power BI update to learn about new features.

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Top Solution Authors
Top Kudoed Authors