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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Anonymous
Not applicable

Infinity Value due to no sales in previous date (holiday or weekend)

Hi,

 

How do I write a code that will choose the previous day that have sales value?

 

Currently I am using the formula as shown below with the "KPI" visual in Power BI to do it but when the previous day are holiday or weekends (which the shop is close) the comparison changes will show infinity.

 

Sales(Today) = calculate(sum([sales]),lastdate([dates]))

Sales(t-1)= calculate(sum([sales]),previousday(lastdate([dates])))

 

Sales(MTD) = TotalMTD(sum([Sales]),[date])

Sales(M-1TD) = calculate(sum([Sales]),DateMTD(DateAdd([date],-1,month)))

 

In the "KPI" visual, I will set the goal/target as Sales(t-1) or Sales(M-1TD) as base of comparison.

 

And how can I do it for MTD vs previous month MTD, and YTD vs previous year YTD?

 

 

 

Please help,

 

7 REPLIES 7
v-haibl-msft
Microsoft Employee
Microsoft Employee

@Anonymous

 

If you have a holiday table like below, we can create a calendar table which only includes working days with following formula. Then add Year and Month column for it.

 

Date_WorkDay = 
CALCULATETABLE (
    Dates,
    FILTER (
        Dates,
        WEEKDAY ( Dates[Dates] ) <> 1
            && WEEKDAY ( Dates[Dates] ) <> 7
            && RELATED ( Holiday[Holiday] ) = BLANK ()
    )
)
Year = YEAR( Date_WorkDay[Dates] )
Month = MONTH( Date_WorkDay[Dates] )

Infinity Value due to no sales in previous date (holiday or weekend)_1.jpg 

 

The next step is to create the measures you wanted.

Sales(Today) = CALCULATE ( SUM ( Table1[Sales] ), LASTDATE ( Date_WorkDay[Dates] ) )
Sales(t-1)_WorkDay = 
VAR LastWorkDay =
    CALCULATE ( MAX ( Date_WorkDay[Dates] ), Date_WorkDay[Dates] < TODAY () )
RETURN
( CALCULATE ( SUM ( Table1[Sales] ), Date_WorkDay[Dates] = LastWorkDay ) )

Infinity Value due to no sales in previous date (holiday or weekend)_2.jpg

 

Sales(MTD) = TOTALMTD ( SUM ( Table1[Sales] ), Date_WorkDay[Dates] )
Sales(M-1TD) = CALCULATE ( SUM ( Table1[Sales] ), PARALLELPERIOD ( Date_WorkDay[Dates], -1, MONTH ) )

Infinity Value due to no sales in previous date (holiday or weekend)_3.jpg

Sales(YTD) = TOTALYTD ( SUM ( Table1[Sales] ), Date_WorkDay[Dates] )
Sales(Y-1TD) = CALCULATE ( SUM ( Table1[Sales] ), PARALLELPERIOD ( Date_WorkDay[Dates], -1, YEAR ) )

Infinity Value due to no sales in previous date (holiday or weekend)_4.jpg

 

Best Regards,

Herbert

Anonymous
Not applicable

Hi Herbert, thanks for replying. How about if I have a table with only working dates. As I have external data as well which will be input into an excel spreadsheet manually everyday.

 

 

 

@Anonymous

 

So there is only working dates in the sales table (Table1)? For example, if 7/21/2016 is not working day, then it will not exist in Table1 as below.

Infinity Value due to no sales in previous date (holiday or weekend)_5.jpg

 

If so, you can use below formula for Sales(t-1).

Sales(t-1) =
VAR LastWorkDay =
    CALCULATE ( MAX ( Table1[Date] ), Table1[Date] < TODAY () )
RETURN
( CALCULATE ( SUM ( Table1[Sales] ), Dates[Date] = LastWorkDay ) )

 

Best Regards,

Herbert

Anonymous
Not applicable

Hi Herbert, I am sorry for the late reply. By using the function "Today()" it will fix the sales amount to today. It disable visual to visual interaction. How can I solve this issue?

@Anonymous

 

I’m not so clear about “It disable visual to visual interaction”. Could you please describe it more clearly?

BTW, you can try with following measure without using Today().

Sales(t-1) = 
VAR LastDay =
    MAX ( Table1[Date] )
VAR LastWorkDay =
    CALCULATE ( MAX ( Table1[Date] ), Table1[Date] < LastDay )
RETURN
    ( CALCULATE ( SUM ( Table1[Sales] ), Table1[Date] = LastWorkDay ) )

 

Best Regards,

Herbert

Anonymous
Not applicable

Hi Herbert,

 

I tried your method, but when I tried to insert the measure into a table (like a pivot table), it returns error. Is there a way to enable it?

 

I am sorry for the late reply, been very busy.

@Anonymous

 

I used this measure in KPI visual as you mentioned before. Which columns do you have in your table and what is the error message returned?

Infinity Value due to no sales in previous date (holiday or weekend)_1.jpg

 

Best Regards,

Herbert

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.