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

DAX showing the last 4 values w/ the same day of week (e.g. Tues only) as my latest day's value

Hi everyone, 

 

I'm currently doing a daily dashboard with a different sales value per day. So basically I want to create a bar visual only showing the sales for the same day the past 4 weeks. For example, today is Monday, I want the visual to show only today, last Monday, Monday 2 weeks ago, Monday 3 weeks ago in a bar. And I want this to be dynamic...so if the next day is a Tuesday, then Tuesday data from the past 4 weeks should show, then Wednesday, only Wednesday and so on. 

 

Is there any DAX formula that I could use to do this? Thank you!!! I've been trying to go this direction but have been failing :

Same DayOfWeek Sales = calculate([Total Sales],
(WEEKDAY([Max Date],2)=WEEKDAY([Total Sales])
 
My logic is : I need to calculate total sales but filtered only to those dates where the weekday (1 to 7) is the same as the max date's weekday. (Plus something to add so I'd only look at the past 4 weeks' data)

I'm a huge beginner at this - only started weeks ago after a few YouTube videos) - so would really appreciate the help! Thank you.
1 ACCEPTED SOLUTION

Hi @chocochoco ,

 

This is because a relationship has been created between the Calendar Date Table and the Volume_0521 table. When you select "5/30/2021" in the slicer, the rows with the "5/30/2021" PUP Date in Volume_0521 table will be filtered out. Then the measure will be calculated for these rows, and the result will return blank value.


If you want to dynamically change according to the date selected in the slicer, you need to delete the relationship between the two tables.

 

vkkfmsft_0-1629710258162.png

 

Then use the following formula:

 

Measure = 
var SelectDate = MAX('Calendar Date Table'[Date])
Return 
    CALCULATE(
        [Total Sales], 
        FILTER(
            'Volume_0521', 
            'Volume_0521'[PUP Date] >= SelectDate - 28
                && WEEKDAY('Volume_0521'[PUP Date], 2) = WEEKDAY( SelectDate, 2 )
        )
    )

vkkfmsft_1-1629710313162.png

 

If the problem is still not resolved, please provide detailed error information or the expected result you expect. Let me know immediately, looking forward to your reply.

Best Regards,
Winniz

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

 

View solution in original post

8 REPLIES 8
parry2k
Super User
Super User

@chocochoco although @v-kkf-msft solution will work I would prefer to use the Time Intelligence function. Make your calendar date table as date table and use the following measure

 

Measure 2 = 
var Today = 
    MAXX( 
        ALL(Volume_0521), 
        Volume_0521[PUP Date] 
    )
VAR LastHowManyWeeks = 5 --including current
Return 
    CALCULATE(
        [Total Sales],         
        KEEPFILTERS ( DATESINPERIOD ( 'Calendar Date Table'[Date], Today, - (LastHowManyWeeks*7 ), DAY ) ),
        'Calendar Date Table'[DayOfWeek] = WEEKDAY( Today, 2 )
    )
    

 

Follow us on LinkedIn

 

Check my latest blog post The Power of Using Calculation Groups with Inactive Relationships (Part 1) (perytus.com) I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

 

Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.

 

 



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Thanks @parry2k and @v-kkf-msft ! Both solutions work, however when I try to pick an older date (for example, a date before the last date of my table), the visual returns a blank. Would you know how to solve this issue? Thanks again

Hi @chocochoco ,

 

This is because a relationship has been created between the Calendar Date Table and the Volume_0521 table. When you select "5/30/2021" in the slicer, the rows with the "5/30/2021" PUP Date in Volume_0521 table will be filtered out. Then the measure will be calculated for these rows, and the result will return blank value.


If you want to dynamically change according to the date selected in the slicer, you need to delete the relationship between the two tables.

 

vkkfmsft_0-1629710258162.png

 

Then use the following formula:

 

Measure = 
var SelectDate = MAX('Calendar Date Table'[Date])
Return 
    CALCULATE(
        [Total Sales], 
        FILTER(
            'Volume_0521', 
            'Volume_0521'[PUP Date] >= SelectDate - 28
                && WEEKDAY('Volume_0521'[PUP Date], 2) = WEEKDAY( SelectDate, 2 )
        )
    )

vkkfmsft_1-1629710313162.png

 

If the problem is still not resolved, please provide detailed error information or the expected result you expect. Let me know immediately, looking forward to your reply.

Best Regards,
Winniz

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

 

Thanks a lot! I think I kind of get it now. I'm still studying the file, but I think my understanding's going to the right direction. 😊

parry2k
Super User
Super User

@chocochoco This is a bit more involved than a simple DAX measure. If you can share pbix file using one drive/google drive, remove sensitive information before sharing, I will put together a solution for you.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

PBIX 

 

Hi @parry2k here's a simple sample of what I intend to do. Hope it's enough!

Hi @chocochoco ,

 

Try the following formula:

 

Measure = 
var Today = 
    MAXX( 
        ALL(Volume_0521), 
        Volume_0521[PUP Date] 
    )
Return 
    CALCULATE(
        [Total Sales], 
        FILTER(
            'Calendar Date Table', 
            'Calendar Date Table'[Date] >= Today - 28
                && 'Calendar Date Table'[DayOfWeek] = WEEKDAY( Today, 2 )
        )
    )

 image.png


If the problem is still not resolved, please provide detailed error information or the expected result you expect. Let me know immediately, looking forward to your reply.

Best Regards,
Winniz

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

Thanks for the prompt response @parry2k ! Appreciate it. Will share the pbix file soon.

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.