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

Need help in dax caulation for this week and last months same week sales

DateSales

10-Aug-2022

100
11-July-202260
08-July-202240
09-July-202222
09-Aug-2022234

I have 2 column Date and Sales. I need to calculate below:

If today date in Aug 10 I need dax for:
1. This week sales-last month's same week sales
i.e sales from 8,9,10 minus sales from July 8,9,10

2. Similary today's sale minus last month's same day sale
i.e sale of Aug 10-sale of July 10

3 REPLIES 3
v-yangliu-msft
Community Support
Community Support

Hi  @kruthikav ,

I created some data:

vyangliumsft_0-1660783914789.png

Here are the steps you can follow:

1. Create calculated column.

Year = YEAR('Table'[Date])
Month = MONTH('Table'[Date])
Week = WEEKNUM('Table'[Date],1)
weeknumber =
RANKX(FILTER(ALL('Table'),
'Table'[Year]=EARLIER('Table'[Year])&&'Table'[Month]=EARLIER('Table'[Month])),[Week],,ASC,Dense)

vyangliumsft_1-1660783914792.png

2. Create measure.

Dax1 =
var _today=TODAY()
var _week=
MAXX(FILTER(ALL('Table'),'Table'[Date]=_today),[weeknumber])
var _weeksales=
SUMX(FILTER(ALL('Table'),'Table'[Month]=MONTH(_today)&&'Table'[weeknumber]=_week),[Amount])
var _lastmonth=
SUMX(FILTER(ALL('Table'),'Table'[Month]=MONTH(_today)-1&&'Table'[weeknumber]=_week),[Amount])
return
_weeksales - _lastmonth
Dax2 =
var _today=TODAY()
var _todaysalse=
SUMX(FILTER(ALL('Table'),'Table'[Date]=_today),[Amount])
var _lastmonthsameday=
SUMX(FILTER(ALL('Table'),'Table'[Date]=DATE(YEAR(_today),MONTH(_today)-1,DAY(_today))),[Amount])
return
_todaysalse - _lastmonthsameday

3. Result:

vyangliumsft_2-1660783914794.png

 

Best Regards,

Liu Yang

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

Jihwan_Kim
Super User
Super User

Hi,

I tried to create a sample pbix file like below.

Please check the below picture and the attached pbix file.

I hope the below can provide some ideas on how to create a solution for your dataset.

 

Picture2.png

 

WTD sales vs previousmonth same dates sales: = 
VAR _wtdtable =
    FILTER (
        ALL ( 'Calendar' ),
        'Calendar'[Date] <= MAX ( 'Calendar'[Date] )
            && 'Calendar'[Start of Week] = MAX ( 'Calendar'[Start of Week] )
    )
VAR _daynumberlist =
    SUMMARIZE ( _wtdtable, 'Calendar'[Day] )
VAR _previousmonthwtdtablestartdate =
    DATE ( IF (
        MAX ( 'Calendar'[Month] ) = 1,
        MAX ( 'Calendar'[Year] ) - 1,
        MAX ( 'Calendar'[Year] )
    ), IF ( MAX ( 'Calendar'[Month] ) = 1, 12, MAX ( 'Calendar'[Month] ) - 1 ), DAY ( MINX ( _wtdtable, 'Calendar'[Date] ) ) )
VAR _previousmonthwtdtablefinishdate =
    DATE ( IF (
        MAX ( 'Calendar'[Month] ) = 1,
        MAX ( 'Calendar'[Year] ) - 1,
        MAX ( 'Calendar'[Year] )
    ), IF ( MAX ( 'Calendar'[Month] ) = 1, 12, MAX ( 'Calendar'[Month] ) - 1 ), DAY ( MAXX ( _wtdtable, 'Calendar'[Date] ) ) )
VAR _currentmonthwtd =
    CALCULATE ( [Sales measure:], _wtdtable )
VAR _previousmonthexpected =
    CALCULATE (
        [Sales measure:],
        DATESBETWEEN (
            'Calendar'[Date],
            _previousmonthwtdtablestartdate,
            _previousmonthwtdtablefinishdate
        )
    )
RETURN
    IF (
        NOT ISBLANK ( CALCULATE ( [Sales measure:], PREVIOUSMONTH ( 'Calendar'[Date] ) ) ),
        _currentmonthwtd - _previousmonthexpected
    )

 

 

sales vs last month same day sales: =
IF (
    NOT ISBLANK (
        CALCULATE ( [Sales measure:], DATEADD ( 'Calendar'[Date], -1, MONTH ) )
    ),
    [Sales measure:]
        - CALCULATE ( [Sales measure:], DATEADD ( 'Calendar'[Date], -1, MONTH ) )
)

 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


daXtreme
Solution Sage
Solution Sage

Hi there

 

You probably thought it'd be easy as 1-2-3.... No, it's not.

Please read this article: Week-Based Time Intelligence in DAX - SQLBI

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