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
Anonymous
Not applicable

value comparison between two dates

Hello Community,

 

Hope you are well.

I am struggling with dates comparison and I would appreciated if you could help me.

I try to build a table that will show the value delta of two projects, broken down per year/quarter, between two different dataset extractions.

 

Table 1 is a simplistic version of my dataset, and table 2 resembles what I would like to produce.

 

Table1 : Raw Data Table

ExtractionDateProjectYearQuarterValue
01/09/2020PR12020195
01/09/2020PR12020281
01/09/2020PR12020370
01/09/2020PR12020440
01/09/2020PR22020132
01/09/2020PR22020274
01/09/2020PR22020357
01/09/2020PR22020465
01/12/2020PR12020195
01/12/2020PR12020281
01/12/2020PR12020380
01/12/2020PR12020469
01/12/2020PR22020132
01/12/2020PR22020274
01/12/2020PR22020373
01/12/2020PR22020480

 

Table2 : Outcome Table

ExtractionDateProjectYearQuarterValueValue Delta
01/12/2020PR120201950
01/12/2020PR120202810
01/12/2020PR1202038010
01/12/2020PR1202046929
01/12/2020PR220201320
01/12/2020PR220202740
01/12/2020PR2202037316
01/12/2020PR2202048015

 

Any help would really be appreciated.

 

Thank you,

 

George

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

Hi, @Anonymous 

 

Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.

Table:

h1.png

 

You may create a calculated column or a measure as below.

Calculated column:

Result Column = 
var edate = [ExtractionDate]
var project = [Project]
var year = [Year]
var quarter = [Quarter]
var _lastdate = 
CALCULATE(
    MAX('Table'[ExtractionDate]),
    FILTER(
        ALL('Table'),
        [ExtractionDate]<edate&&
        [Project]=project&&
        [Year]=year&&
        [Quarter]=quarter
    )
)
var _val = 
CALCULATE(
    SUM('Table'[Value]),
    FILTER(
        ALL('Table'),
        [ExtractionDate]=_lastdate&&
        [Project]=project&&
        [Year]=year&&
        [Quarter]=quarter
    )
)
return
IF(
    ISBLANK(_lastdate),
    BLANK(),
    [Value]-_val
)

 

Measure:

Result Measure = 
var tab = 
ADDCOLUMNS(
    'Table',
    "Result",
    var edate = [ExtractionDate]
    var project = [Project]
    var year = [Year]
    var quarter = [Quarter]
    var _lastdate = 
    CALCULATE(
        MAX('Table'[ExtractionDate]),
        FILTER(
            ALL('Table'),
            [ExtractionDate]<edate&&
            [Project]=project&&
            [Year]=year&&
            [Quarter]=quarter
        )
    )
    var _val = 
    CALCULATE(
        SUM('Table'[Value]),
        FILTER(
            ALL('Table'),
            [ExtractionDate]=_lastdate&&
            [Project]=project&&
            [Year]=year&&
            [Quarter]=quarter
        )
    )
    return
    IF(
        ISBLANK(_lastdate),
        BLANK(),
        [Value]-_val
    )
)
return
SUMX(
    tab,
    [Result]
)

 

Result:

h2.png

 

Best Regards

Allan

 

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

3 REPLIES 3
Anonymous
Not applicable

Hello Allan, @v-alq-msft 

 

Thank you very much for your prompt reply.

 

Your proposal worked perfectly.

 

Please allow me two questions.

 

1) Can you please ellaborate more on the logic behind your solution? For example, I cannot really see at which point the subtraciton between the two extraction dates take place

2) Regarding the 1st extraction date, why are there results againt these rows?

 

Kind regards,

 

George

Hi, @Anonymous 

 

First, we need to calculate last date where 'ExtractDate', 'Project', 'Year', 'Quarter' all equal to the corresponding value in current row.

var _lastdate = 
    CALCULATE(
        MAX('Table'[ExtractionDate]),
        FILTER(
            ALL('Table'),
            [ExtractionDate]<edate&&
            [Project]=project&&
            [Year]=year&&
            [Quarter]=quarter
        )
    )

Then we may calculate the last value according to the last date where 'ExtractDate', 'Project', 'Year', 'Quarter' all equal to the corresponding value in current row.

var _val = 
    CALCULATE(
        SUM('Table'[Value]),
        FILTER(
            ALL('Table'),
            [ExtractionDate]=_lastdate&&
            [Project]=project&&
            [Year]=year&&
            [Quarter]=quarter
        )
    )

 Finally we need to calculate the difference between current value and last value when last value is not blank.

IF(
        ISBLANK(_lastdate),
        BLANK(),
        [Value]-_val
)

 

Hope it helps for you to understand the logic.

 

Best Regards

Allan

 

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

v-alq-msft
Community Support
Community Support

Hi, @Anonymous 

 

Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.

Table:

h1.png

 

You may create a calculated column or a measure as below.

Calculated column:

Result Column = 
var edate = [ExtractionDate]
var project = [Project]
var year = [Year]
var quarter = [Quarter]
var _lastdate = 
CALCULATE(
    MAX('Table'[ExtractionDate]),
    FILTER(
        ALL('Table'),
        [ExtractionDate]<edate&&
        [Project]=project&&
        [Year]=year&&
        [Quarter]=quarter
    )
)
var _val = 
CALCULATE(
    SUM('Table'[Value]),
    FILTER(
        ALL('Table'),
        [ExtractionDate]=_lastdate&&
        [Project]=project&&
        [Year]=year&&
        [Quarter]=quarter
    )
)
return
IF(
    ISBLANK(_lastdate),
    BLANK(),
    [Value]-_val
)

 

Measure:

Result Measure = 
var tab = 
ADDCOLUMNS(
    'Table',
    "Result",
    var edate = [ExtractionDate]
    var project = [Project]
    var year = [Year]
    var quarter = [Quarter]
    var _lastdate = 
    CALCULATE(
        MAX('Table'[ExtractionDate]),
        FILTER(
            ALL('Table'),
            [ExtractionDate]<edate&&
            [Project]=project&&
            [Year]=year&&
            [Quarter]=quarter
        )
    )
    var _val = 
    CALCULATE(
        SUM('Table'[Value]),
        FILTER(
            ALL('Table'),
            [ExtractionDate]=_lastdate&&
            [Project]=project&&
            [Year]=year&&
            [Quarter]=quarter
        )
    )
    return
    IF(
        ISBLANK(_lastdate),
        BLANK(),
        [Value]-_val
    )
)
return
SUMX(
    tab,
    [Result]
)

 

Result:

h2.png

 

Best Regards

Allan

 

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

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.