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
Krib
Helper I
Helper I

How to use neighboring column as basis for calculation?

I have a table of Store Keys ("Ret. Ch.ID"). Some of the Stores is not R12/LFL. Therefore they have another Store Key ("New Ret. Ch. ID") as a replacement in a neighboring column in the same table. This column basically has a Store Key that's equal if the store is R12/LFL, if not it gets the "New Ret. Ch. ID". I want to get the difference in sales for the stores against past year.

The issue is with the newest store (not LFL). This measure work's:  


#PY Sales = CALCULATE ( SUM ( sales[PY_W_D_Sales_Gross] ), ALL ( Store[Retail ch. Region] ), TREATAS ( VALUES( Store[New Ret. Ch. ID] ), Store[Ret. Ch. ID] ) )

The totals doesn't add up and the values doesn't show unless I use the specific "Ret. Ch. ID". Any pointers in the right direction would be great. This is my "simple" test to get the total together (doesnt work):

#test Diff = SUMX(
SUMMARIZE(
    Store,
    Store[Ret. Ch. ID],
    "DIFF", [#Diff Sales]),
    [DIFF]
)

where #Diff sales is:

#Diff Sales = [#Sales]-[#PY Sales]

 

6 REPLIES 6
v-yiruan-msft
Community Support
Community Support

Hi @Krib ,

Could you please provide some sample data in the tables sales and Store and the final result you expect with calculation logic and special examples? Is there any relationship created between tables sales and Store? If so, could you please provide the relevant relationship information (direction, base field and cardinality etc.). Are you trying to get the difference between the current year's sales value and PY sales value? What is the calculation logic of the PY Sales? Thank you.

In addition, you can refer the following links to get the different of current year and last year sales.

How to compare Last year and Current Year sales in Power BI?

Budget vs Actual vs Last Year – Financial Reporting Tips

Best Regards

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

The connection for the model/report is:

Krib_0-1637851425350.pngFrom the "Sales" (named "Values") a sample:

 

Ret. Ch. IDYearYear NoYear WeekYear Week NoSales_GrossPY_W_D_Sales_GrossYesterday_Current week_Past week_Comp. Periode
278202120212021-W4520214541,895 No  YES
278202120212021-W45202145209,79 No  YES
278202120212021-W452021454651,185 No  YES
278202120212021-W452021456846,525 No  YES
278202120212021-W452021451086,3405 No  YES

 

From the "Store"-table a sample of the same ID/key:

 

Retail ch. RegionCountryRet. Ch. IDNew Ret. Ch. IDCountryID
OsloNO2782271

 

 

As mentioned in the initial post i have a measure that calculates the sales:

#Sales = 
SUM('Values'[Sales_Gross])

 and my atempt for PY sales for this example: 

#PY Sales = CALCULATE(
SUM('Values'[PY_W_D_Sales_Gross]),
ALL(Store),
TREATAS(
VALUES(Store[New Ret. Ch. ID]),
Store[Ret. Ch. ID])
)

Then I get the difference with a simple: 

#Diff Sales = [#Sales]-[#PY Sales]

The difference in sales is correct, alone. But when the total appears after including all stores the total diff is significantly off. I've tried an approach that should do the job:

#test Diff = SUMX(
SUMMARIZE(
Store,
Store[Ret. Ch. ID]),
[#Diff Sales]
)

But here I still end up with a diff. Most likely there are more ID/Keys in the "Values" table then in "Stores" and that messes up the total.

When applying that measure to the next level; Region, the total is more obvious wrong

Krib_1-1637852600351.png

Where the "#test diff reg" shows the correct total, in this context but not on a country context.

Measure is equal to:

 

#test Diff = SUMX(
SUMMARIZE(
Store,
Store[Retail ch. Region]),
[#Diff Sales]
)

So, long story short; How to get replacement (new ret ch id) used for PY sales correctly and how to make it sum up in hierarchy?

Hi @Krib ,

You can update your measure [#PY Sales] as below, please find the details in the attachment.

#PY Sales = 
VAR _tab =
    SUMMARIZE (
        'Values',
        'Values'[Ret. Ch. ID],
        "@pysales",
            CALCULATE (
                SUM ( 'Values'[PY_W_D_Sales_Gross] ),
                FILTER (
                    ALL ( 'Values' ),
                    'Values'[Ret. Ch. ID] = SELECTEDVALUE ( 'Store'[New Ret. Ch. ID] )
                )
            )
    )
RETURN
    SUMX ( _tab, [@pysales] )

yingyinr_0-1637920250525.png

Best Regards

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

this is looks good but it doesnt change depending on date. I see that i didnt mention it was sliced through a date slicer. In your measure I cant see how to iterate through the dates

Hi @Krib ,

Please try to update the formula of measure [#PY Sales] as below and check whether it can update the values of visual dynamically base on the date slicer:

#PY Sales =
VAR _tab =
SUMMARIZE (
'Values',
'Values'[Ret. Ch. ID],
"@pysales",
CALCULATE (
SUM ( 'Values'[PY_W_D_Sales_Gross] ),
FILTER (
ALLSELECTED ( 'Values' ),
'Values'[Ret. Ch. ID] = SELECTEDVALUE ( 'Store'[New Ret. Ch. ID] )
)
)
)
RETURN
SUMX ( _tab, [@pysales] )

Best Regards

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

Tried before last post but it wont apply to those stores with replacement ID. Thanks for all your effort.

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.