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
cmc910514
Frequent Visitor

Sum up sales in the latest n days based on sales channel

Hi people, 
I need your help. I have 3 tables and their relationships as the picture below.

cmc910514_0-1679484018024.png


I need to sum up the sales value according to different channel:
Channel A: always sum up sales fromlatest 2 days
Channel B: always sum up sales from latest 2 days
Channel C: always sum up sales from latest day

Do you have any ideas on how I should write the DAX codes? Thanks....


9 REPLIES 9
tamerj1
Super User
Super User

Hi @cmc910514 
Please try

Top N Sales =
SUMX (
    VALUES ( TableA[Channel] ),
    VAR N =
        IF ( TableA[Channel] = C, 1, 2 )
    VAR T =
        TOPN ( N, CALCULATETABLE ( TableC ), TableC[SalesDate] )
    RETURN
        SUMX ( T, TableC[Value] )
)

Hi @tamerj1 , the outcome turns out not correct...but thanks still.

@cmc910514 
Would you please paste some screenshots of your visual along with the DAX that you have used?

Hi @tamerj1 , just realized that I should revise the table C as below. The sales frequency between different channels is different. Really thanks for your help! 

 

04F0FBA4-6A46-4308-9FAD-E8C8C8CB1904.jpeg

@cmc910514 
Anyway, please try

Top N Sales =
SUMX (
    VALUES ( TableA[Channel] ),
    VAR N =
        IF ( TableA[Channel] = C, 1, 2 )
    VAR T =
        TOPN (
            N,
            CALCULATETABLE ( VALUES ( TableC[Sales Date] ), ALL ( TableC[Sales Date] ) ),
            TableC[SalesDate]
        )
    RETURN
        CALCULATE ( SUM ( TableC[Value] ), KEEPFILTERS ( TableC[Sales Date] IN T ) )
)

Hi @tamerj1 , now it's correct for channel A & B, but C doesn't show up...

cmc910514_0-1679497306385.png

 

@cmc910514 

I'm not what could be missing. Please share the sample file. I'll work on it tomorrow morning as I don't have access now to my PC. Hopefully, you'll find the solution before. 

Hi @tamerj1 please try to download the file through the link below. Hope it works...

 

Top N Sales 

@cmc910514 

That should make no difference. Would you please help me out and share the results that you have obtained using my measure? 

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.

Top Solution Authors