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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
rajibmahmud
Helper III
Helper III

Calculation at Row Level

Can someone suggest how can I have Value calculated in a new measure at Row which is not part of Datatable.

 

Below is source table:

 

ProductMeasureMonthValue
ASalesJan100
AGross ProfitJan60
AOperating ProfitJan40
ASalesFeb200
AGross ProfitFeb120
AOperating ProfitFeb90
ASalesMar300
AGross ProfitMar170
AOperating ProfitMar120

 

I need to calculate Gross Margin & OP% from the table and show in below format.

 

 JanFebMar
Sales100200300
Gross Profit60120170
Operating Profit4090120
Gross Margin60.0%60.0%56.7%
OP%40.0%45.0%40.0%
1 ACCEPTED SOLUTION
OpenDataLab
Helper II
Helper II

What you want to do is not as straight forward as it first appears and requires a couple of steps. 

 

1) You need to model your data correctly.  By this I mean you need to transpose the names of the measure into columns

 

2017-07-17_20h10_19.png 

 

 

 

 

2) You need to create individual measure for you Sales, Gross Profit, Operating Profit, Gross Margin and OP%

 

 

Gross Margin Amt = DIVIDE([Gross Profit Amt],[Sales Amt])

Gross Profit Amt = SUM(Data[Gross Profit])

OP% = DIVIDE([Operating Profit Amt],[Sales Amt])

Operating Profit Amt = SUM(Data[Operating Profit])

Sales Amt = SUM(Data[Sales])

 

 

3) You need to create a seperate table with a list of measure names, don't forget a column to denote the sort order so you calculations will be diplayed in the correct order.

 

2017-07-17_20h12_03.png 

 

4) You need to create a measure that will display the correct value.  This is where the magic happens.  This measure will use the SWITCH statement to determine which of the measures (that you created in step 2) to display.

 

 

Measure Display = 
IF (
    HASONEVALUE ( 'Measure'[Measure] ),
    SWITCH ( VALUES ( 'Measure'[Measure] ), 
    "Sales", [Sales Amt],
    "Gross Profit", [Gross Profit Amt],
    "Operating Profit", [Operating Profit Amt],
    "Gross margin",FORMAT( [Gross Margin Amt], "Percent"),
    "OP%",FORMAT([OP%],"Percent")
    )
)

 

I have attached a Power BI workbook with a working solution here: One Drive

 

or you can take a look here: Power BI Publish to Web

 

2017-07-17_20h15_54.png

 

 

View solution in original post

8 REPLIES 8
OpenDataLab
Helper II
Helper II

What you want to do is not as straight forward as it first appears and requires a couple of steps. 

 

1) You need to model your data correctly.  By this I mean you need to transpose the names of the measure into columns

 

2017-07-17_20h10_19.png 

 

 

 

 

2) You need to create individual measure for you Sales, Gross Profit, Operating Profit, Gross Margin and OP%

 

 

Gross Margin Amt = DIVIDE([Gross Profit Amt],[Sales Amt])

Gross Profit Amt = SUM(Data[Gross Profit])

OP% = DIVIDE([Operating Profit Amt],[Sales Amt])

Operating Profit Amt = SUM(Data[Operating Profit])

Sales Amt = SUM(Data[Sales])

 

 

3) You need to create a seperate table with a list of measure names, don't forget a column to denote the sort order so you calculations will be diplayed in the correct order.

 

2017-07-17_20h12_03.png 

 

4) You need to create a measure that will display the correct value.  This is where the magic happens.  This measure will use the SWITCH statement to determine which of the measures (that you created in step 2) to display.

 

 

Measure Display = 
IF (
    HASONEVALUE ( 'Measure'[Measure] ),
    SWITCH ( VALUES ( 'Measure'[Measure] ), 
    "Sales", [Sales Amt],
    "Gross Profit", [Gross Profit Amt],
    "Operating Profit", [Operating Profit Amt],
    "Gross margin",FORMAT( [Gross Margin Amt], "Percent"),
    "OP%",FORMAT([OP%],"Percent")
    )
)

 

I have attached a Power BI workbook with a working solution here: One Drive

 

or you can take a look here: Power BI Publish to Web

 

2017-07-17_20h15_54.png

 

 

Hi @OpenDataLab  Is there a possibility that we can implement this way for a matrix? I have a similar requirement where as I have two levels of matrix defining this way. 

I cant see the file in Onedrive 😞

Please try downloading from this link: One Drive.  If this is what your looking for, please mark as Solution.

 

Let me know how you get on.

 

@OpenDataLab

 

This will do perfectly. However I was wondering will it be possible to do it without transposing the table as I will need this for other purpose. 

 

One option could be if I can use something similar to SUMIFS of Excel to calculate below. I am new in DAX, so I am not sure whether there is option to do that or not...

 

Gross Margin Amt = DIVIDE([Gross Profit Amt],[Sales Amt])

Gross Profit Amt = SUM(Data[Gross Profit])

OP% = DIVIDE([Operating Profit Amt],[Sales Amt])

Operating Profit Amt = SUM(Data[Operating Profit])

Sales Amt = SUM(Data[Sales])

 

I made it work by adding entire formula in a calculated measure, where I used Calculate to get value from table and for calculate measure entire divide formula is written using Divide & calculate combination. Now my measure have 2 pages long formula but its working.

Greg_Deckler
Super User
Super User

I solved a similar issue here:

https://community.powerbi.com/t5/Desktop/This-week-in-Dax/td-p/211393

 


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
OpenDataLab
Helper II
Helper II

What you want to do is not as straight forward as it first appears and requires a couple of steps. 

 

1) You need to model your data correctly.  By this I mean you need to transpose the names of the measure into columns

 

2017-07-17_20h10_19.png 

 

 

 

 

2) You need to create individual measure for you Sales, Gross Profit, Operating Profit, Gross Margin and OP%

 

 

Gross Margin Amt = DIVIDE([Gross Profit Amt],[Sales Amt])

Gross Profit Amt = SUM(Data[Gross Profit])

OP% = DIVIDE([Operating Profit Amt],[Sales Amt])

Operating Profit Amt = SUM(Data[Operating Profit])

Sales Amt = SUM(Data[Sales])

 

 

3) You need to create a seperate table with a list of measure names, don't forget a column to denote the sort order so you calculations will be diplayed in the correct order.

 

2017-07-17_20h12_03.png 

 

4) You need to create a measure that will display the correct value.  This is where the magic happens.  This measure will use the SWITCH statement to determine which of the measures (that you created in step 2) to display.

 

 

Measure Display = 
IF (
    HASONEVALUE ( 'Measure'[Measure] ),
    SWITCH ( VALUES ( 'Measure'[Measure] ), 
    "Sales", [Sales Amt],
    "Gross Profit", [Gross Profit Amt],
    "Operating Profit", [Operating Profit Amt],
    "Gross margin",FORMAT( [Gross Margin Amt], "Percent"),
    "OP%",FORMAT([OP%],"Percent")
    )
)

 

I have attached a Power BI workbook with a working solution here: One Drive

 

or you can take a look here: Power BI Publish to Web

 

2017-07-17_20h15_54.png

 

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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