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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
elmurat
Helper I
Helper I

When I select a date, also show previous 3 dates.

Hello,

 

Please see the attached pbix for dummy data.

I need to create a dashboard, where the user selects one date from the date slicer (let's say 11/25/2024), and the table visual will show these 4 dates:

1. Selected date (11/25/2024)

2. 7 days ago (11/18/2024)

3. 14 days ago (11/11/2024)

4. 21 days ago (11/04/2024)

1 ACCEPTED SOLUTION
v-mengmli-msft
Community Support
Community Support

Hi @elmurat ,

 

According to your description, you could try this method.

1. Generate a calendar table based on the maximum and minimum dates of the Date column.

CalendarTable = 
VAR MinDate = MINX(ALL('Table'),'Table'[Date])
VAR MaxDate = MAXX(ALL('Table'),'Table'[Date])
RETURN
ADDCOLUMNS(
    CALENDAR(MinDate, MaxDate),
    "Year", YEAR([Date]),
    "Month", MONTH([Date]),
    "Day", DAY([Date]),
    "Quarter", QUARTER([Date]),
    "Weekday", WEEKDAY([Date])
)

2. Create a DAX measure to generate a virtual table with an interval of seven days, and determine whether the date of the date column is in the virtual table.

Tag = 
VAR SelectedDate = SELECTEDVALUE('CalendarTable'[Date]) 
VAR DateTable =
ADDCOLUMNS(
    GENERATESERIES(0, 3, 1),
    "Date", SelectedDate - [Value] * 7
)
RETURN IF(ISBLANK(SelectedDate),1,if(CONTAINS(DateTable,[Date],MAX('Table'[Date])),1,0))

3. Set Filters.

vmengmlimsft_0-1733816379702.png

 

 

 

 

 

Best regards,

Mengmeng Li

View solution in original post

11 REPLIES 11
Bibiano_Geraldo
Resident Rockstar
Resident Rockstar

Hello, @elmurat!

This is an excellent question. I’m going to use a trick to ensure you get exactly the result you want.

Why am I using tricks? Because we have two main reasons for this:

  1. When the user selects a date in the slicer, the Matrix is automatically filtered to that specific date, but what you really want is the selected date plus the rows with dates 7, 14, and 21 days prior to the selected date, included in the Matrix.

  2. You might think: "Why not use functions like ALL or ALLSELECTED to remove filters?" The issue is that by doing this, the slicer filters will be ignored, which is not what you want. The selected value on the slicer need to be considered for calculations, and those functions do not achieve that.

Now, let's get to work!

Let's assume you're in a scenario like the following:

  1. You have a fact table, where the sales data, for example, is stored.
  2. You also have a calendar dimension table and marked as date calendar

What do you need to do:

1. Duplicate the calendar table using the following DAX (replace the table name with the correct name of your calendar table):

Calendar2 = 'Calendar' -- Make sure to replace the name with the name of your calendar table

 

2. Create the measure with the following DAX:

Measure = 
-- Defining the selected date
VAR SelectedDate = 
    CALCULATE(
        MAX('Calendar'[Date]), 
        ALLSELECTED('Calendar'[Date])  -- Gets the selected date in the slicer, considering the slicer filter
    )

-- Defining the dates 7, 14, and 21 days before the selected date
VAR DateMinus7 = SelectedDate - 7
VAR DateMinus14 = SelectedDate - 14
VAR DateMinus21 = SelectedDate - 21

-- Returning 1 if the date in Calendar2 matches the selected date or any of the previous dates, otherwise returning 0
RETURN
IF(
    SELECTEDVALUE('Calendar2'[Date]) = SelectedDate ||
    SELECTEDVALUE('Calendar2'[Date]) = DateMinus7 ||
    SELECTEDVALUE('Calendar2'[Date]) = DateMinus14 ||
    SELECTEDVALUE('Calendar2'[Date]) = DateMinus21,
    1,
    0
)

 

3. Ensure the two calendar tables are related to the fact table. The relationship is important to ensure the matrix shows the data correctly. Example below:

Bibiano_Geraldo_0-1733830501670.png

 

4. Add a slicer to your report using the date column from the original Calendar table.

 

5. Add a matrix to your report and add the date column from the Calendar2 table (the duplicated table referencing the original calendar table) to the Rows field of the matrix.

 

6.In the Filters pane, with the matrix selected, add the measure you created to the Filters on this visual field. In the Show items when the value field, select is 1, and apply the filter.

 

Now, your table will return the results as expected!

Note: If nothing is selected in the slicer, it will default to considering the last date in your report.

 

See the result below:

ezgif-7-f1695f0b8d.gif

Download the sample here

 

 

 

I hope this helps! 
If you found this answer helpful:
✔️ Mark it as the solution to help others find it faster.
 Give it a like to show your appreciation!

Thank you for contributing to our amazing Power BI community! 

Thank you@Bibiano_Geraldo, for your help. I tried to recreate your solution, but when I put the Sales into the matrix, the dates other than the selected ones are getting removed. 

Hi, 

Please share no sensitive sample file

I hope this helps! 
If you found this answer helpful:
✔️ Mark it as the solution to help others find it faster.
 Give it a like to show your appreciation!

Thank you for contributing to our amazing Power BI community! 

Hi Bibiano, here is the link to the test pbix file:  TestPBIX.pbix. Let me know if you can't access the file.

 

Hi @elmurat ,

To achieve desired result asked in the file, please create the following measure:

For Diference:

Diference = 
VAR totalWeek = 
    CALCULATE(
        SUM(Weekly[Count]),
        FILTER(
            Weekly,
            Weekly[4 Weeks Filter] = 1
        )
    )

VAR vDate = SELECTEDVALUE(Calendar_Table[Date]) - 28

VAR totalMonth = 
    CALCULATE(
        SUM(Monthly[Count]),
        FILTER(
            Monthly,
            Monthly[Date] = vDate
        )
    )

RETURN
    totalWeek - totalMonth


For Diference %

Diference % = 
VAR totalWeek = 
    CALCULATE(
        AVERAGE(Weekly[Percent]),
        FILTER(
            Weekly,
            Weekly[4 Weeks Filter] = 1
        )
    )

VAR vDate = SELECTEDVALUE(Calendar_Table[Date]) - 28

VAR totalMonth = 
    CALCULATE(
        AVERAGE(Monthly[Percent]),
        FILTER(
            Monthly,
            Monthly[Date] = vDate
        )
    )

RETURN
    totalWeek - totalMonth

 

Your output will look like this:

Bibiano_Geraldo_0-1734006700334.png

 

I hope this helps! 
If you found this answer helpful:
✔️ Mark it as the solution to help others find it faster.
 Give it a like to show your appreciation!

Thank you for contributing to our amazing Power BI community! 
v-mengmli-msft
Community Support
Community Support

Hi @elmurat ,

 

According to your description, you could try this method.

1. Generate a calendar table based on the maximum and minimum dates of the Date column.

CalendarTable = 
VAR MinDate = MINX(ALL('Table'),'Table'[Date])
VAR MaxDate = MAXX(ALL('Table'),'Table'[Date])
RETURN
ADDCOLUMNS(
    CALENDAR(MinDate, MaxDate),
    "Year", YEAR([Date]),
    "Month", MONTH([Date]),
    "Day", DAY([Date]),
    "Quarter", QUARTER([Date]),
    "Weekday", WEEKDAY([Date])
)

2. Create a DAX measure to generate a virtual table with an interval of seven days, and determine whether the date of the date column is in the virtual table.

Tag = 
VAR SelectedDate = SELECTEDVALUE('CalendarTable'[Date]) 
VAR DateTable =
ADDCOLUMNS(
    GENERATESERIES(0, 3, 1),
    "Date", SelectedDate - [Value] * 7
)
RETURN IF(ISBLANK(SelectedDate),1,if(CONTAINS(DateTable,[Date],MAX('Table'[Date])),1,0))

3. Set Filters.

vmengmlimsft_0-1733816379702.png

 

 

 

 

 

Best regards,

Mengmeng Li

hi @v-mengmli-msft , can you please help me build the next part of my calculation?

The next thing I need to build is the Difference from the Monthly number.

Please see attached test pbix file:  TestPBIX.pbix

thank you very much! This worked!

elmurat
Helper I
Helper I

elmurat
Helper I
Helper I

The table will look like this

 

elmurat_0-1733771926969.png

 

Hi,

  1. Create a Calendar Table
  2. Create a relationship (Many to One and Single) from the Date column of the Calendar Table to the Date column of the Calendar Table
  3. To your visual, drag the Date column from the Calendar Table
  4. Write these measures

Total sales = sum(Data[Sales])

Total sales 7 days ago = calculate([Total sales],datesbetween(calendar[date],min(calendar[date])-6,min(calendar[date])))

Hope this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

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!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

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