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
Ani26
Helper III
Helper III

Fetch previous date value if an entry is missing for a said date.

Hi Team,

Hope you all are doing good!!

 

I have a requirement that I need to show exchange rates for all currencies in front of the dates in a calendar table where the day is either Friday or a month end. 

Let me explain it to you with sample data and expected output.

Table1 : Rate

FromCurrency EffectiveDate ExchangeRate
AED3/15/20200.27225
AED3/16/20200.27223
AED3/17/20200.27225
AED3/18/20200.27225
AED3/19/20200.27223
ZTWD3/15/20200.03314
ZTWD3/16/20200.03315
ZTWD3/17/20200.03309
ZTWD3/18/20200.03315
ZTWD3/19/20200.03314
ZTWD3/20/20200.03314

 

Table2 : Calendar

DateWeekNumberWeekFillupFlagInfo
3/15/2020 0:001203/13/20200
3/16/2020 0:001203/20/20200
3/17/2020 0:001203/20/20200
3/18/2020 0:001203/20/20200
3/19/2020 0:001203/20/20200
3/20/2020 0:001203/20/20201
3/21/2020 0:001203/20/20200


Requirement:

I want to have a table with following columns:

DateEffectiveDateFromCurrencyWeekNumber WeekFillup FlagInfo Rate


But the Rate should be the value from the Rate table where WeekFillup = EffectiveDate.

For this I merged the two tables selecting the above columns for joining and I got 50% of the requirement.

 

Now suppose, due to a holiday in UAE there was no entry in the Rate table for 03/20/2020 which is a Friday with FlagInfo value as 1 (FlagInfo value is a column which has a value 1 when the day is either a Friday or month end day else it is 0).
But in the output table in front of the Date column having value as 03/20/2020 we want the Rate column to have a previous date value (03/19/2020) from the Rate table. This is something where I am stucked. I understand that I may need to use CROSSJOIN function but not able to apply it.

 

The expected output is as below where the bold values are actually the rate for 03/19/2020 as the record was missing for 20th March for AED but it worked as expected for ZTWD as there was no entry missing for 20th March.

DateEffectiveDateFromCurrencyWeekNumber WeekFillup FlagInfo Rate
3/15/2020 3/15/2020AED123/13/202000.27225
3/16/20203/16/2020AED123/20/202000.27223
3/17/20203/17/2020AED123/20/202000.27223
3/18/20203/18/2020AED123/20/202000.27223
3/19/20203/19/2020AED123/20/202000.27223
3/20/20203/20/2020AED123/20/202010.27223
3/15/2020 3/15/2020ZTWD123/13/202000.03314
3/16/20203/16/2020ZTWD123/20/202000.03314
3/17/20203/17/2020ZTWD123/20/202000.03314
3/18/20203/18/2020ZTWD123/20/202000.03314
3/19/20203/19/2020ZTWD123/20/202000.03314
3/20/20203/20/2020ZTWD123/20/202010.03314


I hope I was able to explain it properly. If in case any further information required do let me know.

 

Thanks,

Ani

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

Hi @Ani26 ,

 

 

We can create a table to meet your requirement.

 

Table =
ADDCOLUMNS (
    CROSSJOIN ( DISTINCT ( 'Rate'[FromCurrency] ), 'Calendar' ),
    "EffectiveDate", [Date],
    "Rate",
    VAR result =
        CALCULATE (
            SUM ( 'Rate'[ExchangeRate] ),
            'Rate'[FromCurrency] = EARLIER ( [FromCurrency] ),
            'Rate'[EffectiveDate] = EARLIER ( [Date] )
        )
    RETURN
        IF (
            result = BLANK ()
                && [FlagInfo] = 1,
            VAR lastD =
                CALCULATE (
                    MAX ( 'Rate'[EffectiveDate] ),
                    'Rate'[FromCurrency] = EARLIER ( [FromCurrency] ),
                    'Rate'[EffectiveDate] < EARLIER ( [Date] )
                )
            RETURN
                CALCULATE (
                    SUM ( 'Rate'[ExchangeRate] ),
                    'Rate'[FromCurrency] = EARLIER ( [FromCurrency] ),
                    'Rate'[EffectiveDate] = lastD
                ),
            result
        )
)

 

33.png

 

BTW, pbix as attached.


Best regards,

Community Support Team _ Dong Li
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

2 REPLIES 2
v-lid-msft
Community Support
Community Support

Hi @Ani26 ,

 

 

We can create a table to meet your requirement.

 

Table =
ADDCOLUMNS (
    CROSSJOIN ( DISTINCT ( 'Rate'[FromCurrency] ), 'Calendar' ),
    "EffectiveDate", [Date],
    "Rate",
    VAR result =
        CALCULATE (
            SUM ( 'Rate'[ExchangeRate] ),
            'Rate'[FromCurrency] = EARLIER ( [FromCurrency] ),
            'Rate'[EffectiveDate] = EARLIER ( [Date] )
        )
    RETURN
        IF (
            result = BLANK ()
                && [FlagInfo] = 1,
            VAR lastD =
                CALCULATE (
                    MAX ( 'Rate'[EffectiveDate] ),
                    'Rate'[FromCurrency] = EARLIER ( [FromCurrency] ),
                    'Rate'[EffectiveDate] < EARLIER ( [Date] )
                )
            RETURN
                CALCULATE (
                    SUM ( 'Rate'[ExchangeRate] ),
                    'Rate'[FromCurrency] = EARLIER ( [FromCurrency] ),
                    'Rate'[EffectiveDate] = lastD
                ),
            result
        )
)

 

33.png

 

BTW, pbix as attached.


Best regards,

Community Support Team _ Dong Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thank you so much @v-lid-msft . This also worked as expected. Also I tried one more approach and even that fetched me the required output. But I think this is more covenient. Thank you so much again.

 

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.