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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
pbi_new_user
Frequent Visitor

Measure to calculate sum by comparing 2 columns and reassigning to another row

Hi,

 

I have a table with sales amount for different stores along with the storenumber from where the sale originated.

 

CountryStoreStore originalAmount
AS1O1100
AS1 100
AS2O1100
AS2 50
AS2O150
AO1 100
  Total500
BS1O2100
BS1 100
BS2O2100
BS2O250
BO2 100
  Total450

 

I need to create a measure to calculate the sales for each corresponding store and the output needs to be like below,

 

CountryStoretotal Sales
AS1100
AS250
AO1350
 Total500
BS1100
BS2 
BO2350
 Total450
8 REPLIES 8
v-zhangti
Community Support
Community Support

Hi, @pbi_new_user 

 

You can try the following methods.

Column:

New Store Column = IF([Store original]<>BLANK(),[Store original],[Store])

vzhangti_0-1682492310481.png

Result:

vzhangti_1-1682492340322.png

Is this the result you expect?

 

Best Regards,

Community Support Team _Charlotte

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

Thanks for the quick reply @v-zhangti .

 

This will work, but I cannot create a calculated column as I connect to a tabular model. Also I need to also calcualte the original sales data along with the corrected sales like below,

 

CountryStoreSales AmountCorrected Sales
AS1200100
AS220050
AO1100350
BS1200100
BS2150 
BO2100350

 

So I need to write this as a measure.

Hi, @pbi_new_user 

You can try the following methods.
Measure:

Sales Amount = SUM('Table'[Amount])
Measure 1 = 
Var _table=FILTER(SUMMARIZE('Table','Table'[Country],'Table'[Store original],"Sum",SUM('Table'[Amount])),[Store original]<>BLANK())
Return
SUMX(_table,[Sum])
Measure 2 = SUMX(FILTER(ALL('Table'),[Country]=MAX('Table'[Country])),[Measure 1])
Measure 3 = MAXX(FILTER(ALL('Table'),[Country]=MAX('Table'[Country])),[Store original])
Corrected Sales = 
Var _N1=CALCULATE(SUM('Table'[Amount]),FILTER(ALL('Table'),[Store original]=BLANK()&&[Country]=MAX('Table'[Country])&&[Store]=MAX('Table'[Store])))
Return
IF(MAX('Table'[Store])=[Measure 3],[Sales Amount]+[Measure 2],_N1)

vzhangti_0-1682559548826.png

Is this the result you expect?

 

Best Regards,

Community Support Team _Charlotte

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

Hi @v-zhangti ,

 

I managed to replicate these measures in my data model but only issue is that I want to also see the totals on the country level which is wrong or not showing up. Please help.

 

My model:

 

pbi_new_user_0-1683192990722.png

 

Result in matrix visual: 

 

pbi_new_user_0-1683192888391.png

 

Hi, @pbi_new_user 

 

You can try the following methods.
Measure:

Result = Var _table=SUMMARIZE('Table','Table'[Country],'Table'[Store],"Sum",[Corrected Sales])
Return
IF(HASONEVALUE('Table'[Store]),[Corrected Sales],SUMX(_table,[Sum]))

vzhangti_0-1683193448591.png

Is this the result you expect?

 

Best Regards,

Community Support Team _Charlotte

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

 

Thanks for the help @v-zhangti .

 

I solved this by creating one measure instead of so many measures and my solution is much cleaner.

 

pbi_new_user_1-1683195881210.png

 

 

 

 

Correct Sales =
sum(Sales[Amount])
   + SUMX (
        FILTER (
            CALCULATETABLE ( 'Sales', REMOVEFILTERS ( 'Store' ) ),
            CONTAINS ( 'Store', [Store], 'Sales'[Store Original] )
        ),
        [Amount]
    )
   - SUMX (
        FILTER ( 'Sales', 'Sales'[Store Original] <> BLANK() ),
        [Amount]
    )

 

 

Thanks for the solution @v-zhangti . 

 

My data is really huge and sales table only has the store and store_original columns. The country column exists in a seperate store dimension. I am not able to replicate these measures there.

Hi @v-zhangti ,

To simplify the above scenario, this is how my sales table looks like,

 

pbi_new_user_1-1682492604538.png

 And I want the output as below,

 

StoreSales Amount
S120
S230
S340
S450
O180
O260

 

Many thanks for your help.

 

 

 

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.