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
Zbig62
Regular Visitor

Value 7 days earlier than selected

Hello everyone

Please help

I want to get information about the number of measurements (count of result) 7 days earlier than the selected day

The table with sample data looks like this:

Zbig62_0-1727377651268.png

For example, I select the day: 2024-02-01 where there were 16 measurements and I need to create a measure showing how many measurements there were 7 days earlier

 

Zbig62_1-1727377691796.png

The measure I wrote gives a "Blank" result every time. Where is the error?

 

DAY - 7 =

var a = SELECTEDVALUE('public Measurement'[DAY])

RETURN

CALCULATE(

COUNT('public Measurement'[result]); FILTER('public Measurement'; 'public  Measurement'[DAY] = a - 7

              )

        )

4 ACCEPTED SOLUTIONS

You have to change the filter in your expression

DAY - 7 =

CALCULATE (
    COUNT ( 'public Measurement'[result] ),
    FILTER (
        'public Measurement',
        'public  Measurement'[DAY]
            = DATEADD ( SELECTEDVALUE ( 'public Measurement'[DAY] ), -7, DAY )
    )
)


Regards,
Sachin
Check out my Blog

View solution in original post

I have a problem with the type of parameters in and I don't know how to deal with it

Zbig62_0-1727413571956.png

 

View solution in original post

v-xianjtan-msft
Community Support
Community Support

Hi @Zbig62 

 

I don't think you should use the “DAY” field in the "public Measurement" table as a slicer, please create a calendar table to use as a slicer. And don't create a relationship between the calendar table and the "public Measurement" table.

1. Create a calendar table and use it as a slicer.

 

Calendar = DISTINCT('public Measurement'[DAY])

 

 2. Replace the field “DAY” in your measure with the field “DAY” of calendar table.

 

DAY - 7 = 
CALCULATE(
    COUNT('public Measurement'[result]),
    FILTER(
        'public Measurement',
        'public Measurement'[DAY] = SELECTEDVALUE('Calendar'[DAY]) - 7
    )
)

 

 

Here is my simple test result:

vxianjtanmsft_0-1727427417068.png

By the way, when the day DAY-7 does not exist for the "DAY" in your data, the measure will return blank. For example, if you slicer selects 2024-02-01, DAY-7 is 2024-1-25, because the data for 2024-1-25 does not exist in your table, the measure returns blank.

 

Best Regards,
Jarvis Tang
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

Sorry I didnt realise that Dateadd function needs an exclusive date table.Your approach wouldnt work as you are trying to compare rows within the current row context and subtracting from the same value using a filter.

As suggested by @v-xianjtan-msft you need a date table. You can use the solution provided by @v-xianjtan-msft 



Regards,
Sachin
Check out my Blog

View solution in original post

7 REPLIES 7
v-xianjtan-msft
Community Support
Community Support

Hi @Zbig62 

 

I don't think you should use the “DAY” field in the "public Measurement" table as a slicer, please create a calendar table to use as a slicer. And don't create a relationship between the calendar table and the "public Measurement" table.

1. Create a calendar table and use it as a slicer.

 

Calendar = DISTINCT('public Measurement'[DAY])

 

 2. Replace the field “DAY” in your measure with the field “DAY” of calendar table.

 

DAY - 7 = 
CALCULATE(
    COUNT('public Measurement'[result]),
    FILTER(
        'public Measurement',
        'public Measurement'[DAY] = SELECTEDVALUE('Calendar'[DAY]) - 7
    )
)

 

 

Here is my simple test result:

vxianjtanmsft_0-1727427417068.png

By the way, when the day DAY-7 does not exist for the "DAY" in your data, the measure will return blank. For example, if you slicer selects 2024-02-01, DAY-7 is 2024-1-25, because the data for 2024-1-25 does not exist in your table, the measure returns blank.

 

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

 

Zbig62
Regular Visitor

Hello
Thank you for your quick response. I have a small problem with this function. If I use it in a variable definition
I get the message SYNTAX ERROR

day - 7 =

a = DATEADD(SELECTEDVALUE('public Measurement'[DAY]),-7,day)

Return

CALCULATE(

COUNT('public Measurement'[result]); FILTER('public Measurement'; 'public  Measurement'[DAY] = a 

              )

        )

You have to change the filter in your expression

DAY - 7 =

CALCULATE (
    COUNT ( 'public Measurement'[result] ),
    FILTER (
        'public Measurement',
        'public  Measurement'[DAY]
            = DATEADD ( SELECTEDVALUE ( 'public Measurement'[DAY] ), -7, DAY )
    )
)


Regards,
Sachin
Check out my Blog

I have a problem with the type of parameters in and I don't know how to deal with it

Zbig62_0-1727413571956.png

 

I tried another approach and I also get a message about parameter type mismatch

Zbig62_1-1727418875897.png

 

 

Sorry I didnt realise that Dateadd function needs an exclusive date table.Your approach wouldnt work as you are trying to compare rows within the current row context and subtracting from the same value using a filter.

As suggested by @v-xianjtan-msft you need a date table. You can use the solution provided by @v-xianjtan-msft 



Regards,
Sachin
Check out my Blog
SachinNandanwar
Impactful Individual
Impactful Individual

DATEADD(SELECTEDVALUE('public Measurement'[DAY]),-7,day)



Regards,
Sachin
Check out my Blog

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!

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.