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%
2 REPLIES 2
v-yulgu-msft
Employee
Employee

Hi @rajibmahmud,

 

You could try this workaround.

 

First, add two calculated columns in source table. (In my test, it's Table-2)

Gross Margin =
IF (
    'Table-2'[Measure] = "Gross Profit",
    (
        CALCULATE (
            SUM ( 'Table-2'[Value] ),
            ALLEXCEPT (
                'Table-2',
                'Table-2'[Product],
                'Table-2'[Measure],
                'Table-2'[Month]
            )
        )
            / CALCULATE (
                SUMX ( FILTER ( 'Table-2', 'Table-2'[Measure] = "Sales" ), 'Table-2'[Value] ),
                ALLEXCEPT ( 'Table-2', 'Table-2'[Product], 'Table-2'[Month] )
            )
    ),
    BLANK ()
)

OP% =
IF (
    'Table-2'[Measure] = "Operating Profit",
    (
        CALCULATE (
            SUM ( 'Table-2'[Value] ),
            ALLEXCEPT (
                'Table-2',
                'Table-2'[Product],
                'Table-2'[Measure],
                'Table-2'[Month]
            )
        )
            / CALCULATE (
                SUMX ( FILTER ( 'Table-2', 'Table-2'[Measure] = "Sales" ), 'Table-2'[Value] ),
                ALLEXCEPT ( 'Table-2', 'Table-2'[Product], 'Table-2'[Month] )
            )
    ),
    BLANK ()
)

Then, create a new table.

Table_3 =
UNION (
    SELECTCOLUMNS (
        'Table-2',
        "Measure", 'Table-2'[Measure],
        "Month", 'Table-2'[Month],
        "Value", 'Table-2'[Value]
    ),
    SELECTCOLUMNS (
        'Table-2',
        "Measure", "Gross Margin",
        "Month", 'Table-2'[Month],
        "Value", 'Table-2'[Gross Margin]
    ),
    SELECTCOLUMNS (
        'Table-2',
        "Measure", "OP%",
        "Month", 'Table-2'[Month],
        "Value", 'Table-2'[OP%]
    )
)

You can use a matrix to display data from Table_3.

 

Regards,
Yuliana Gu

Community Support Team _ Yuliana Gu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Phil_Seamark
Employee
Employee

Hi @rajibmahmud

 

I'd recommend pivoting your 2nd table to be a format that looks more like this.  Use the filter and unpivot functions in the query editor to help.  Then you can create DAX measures (or merge the tables in the Query Editor)

 

Gross Profit    Jan      60
Gross Profit    Feb     120
Gross Profit    Mar     170
Op. Profit      Jan      40
Op. Profit      Feb      90
Op. Profit      Mar     120

To learn more about DAX visit : aka.ms/practicalDAX

Proud to be a Datanaut!

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.