cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
aBerg Regular Visitor
Regular Visitor

Days calculating error

Hi. 

 

I'm trying to make a calculation based on the difference between two dates (two days), but the result is not the one that is expected (in red), but shows as "infinite". 

It seems to me that the interval between days (Interval Days measure) despite being shown correctly on the card (30 days) for the calculation is considering as zero, so the result as "infinite". Smiley Frustrated

 

Could someone help me by pointing out a way to solve this error ? 

 

Greetings !

 

Days Calc.png

1 ACCEPTED SOLUTION

Accepted Solutions
Super User
Super User

Re: Days calculating error

Hi @aBerg

 

Try this for your measure, placing it in the values of a matrix with Month and Day on rows as you show. Table1 is the table containing Month and Day columns shown in the matrix visual.

 

 

Calc Target =
SELECTEDVALUE ( Table1[Day] )
    * DIVIDE (
        CALCULATE ( COUNT ( Table1[Day] ) - 1, ALL ( Table1[Day] ) ),
        [BaseValue]
    )

 

Code formatted with   www.daxformatter.com

11 REPLIES 11
Super User
Super User

Re: Days calculating error

Hi @aBerg

 

Try this for your measure, placing it in the values of a matrix with Month and Day on rows as you show. Table1 is the table containing Month and Day columns shown in the matrix visual.

 

 

Calc Target =
SELECTEDVALUE ( Table1[Day] )
    * DIVIDE (
        CALCULATE ( COUNT ( Table1[Day] ) - 1, ALL ( Table1[Day] ) ),
        [BaseValue]
    )

 

Code formatted with   www.daxformatter.com

aBerg Regular Visitor
Regular Visitor

Re: Days calculating error

Hi @AlB

 

Thank you for the help so fast. Smiley Very Happy

Could you tell me why the code I have posted did not work? Just to understand the reason ... 

 

Tks

Super User
Super User

Re: Days calculating error

@aBerg

Smiley Happy You're welcome.

I could, if you share the pbix. Otherwise we'd have to spend too much time while I ask about details omitted in your explanation.

In any case, my current guess would be as follows:

Fact: the division yields infinite,

so [Interval Days] must be zero,

which means [Initial Day] = [Last Day],

which implies that MIN(dCalendario[Data]) = MAX(dCalendario[Data]),

from which we can deduce that the day number for dCalendario[Data] has a sole distinct value in the filter context when the measure is evaluated.

 

You have Month and Day in the rows of your visual. That defines a sole day number value in dCalendario[Data].

     

 

Super User
Super User

Re: Days calculating error

@aBerg

 

Was I clear enough?

aBerg Regular Visitor
Regular Visitor

Re: Days calculating error

Hi @AlB !

 

Sorry for the delay in reply, I was traveling on the weekend.

 

Thank you, I understood your explanation, but I do not understand why the interval of days on the card is normally displayed, but when I use it in a measure the interval considered is zero. : smileyfrustrated:
Ok I'll check to share the PBIX here ... 

 

Greetings !

aBerg Regular Visitor
Regular Visitor

Re: Days calculating error

Hi @AlB !

 

Below following the link for PBIX app:

 

https://1drv.ms/u/s!Ag06VMKBGvfCgixvcV3SbuZPwVIg

 

 

Super User
Super User

Re: Days calculating error

Hi @aBerg

 

You have a table visual with Year, Month and Day, all columns of table dCalendar. All three fields will be creating a filter context for the measures you include later in the table visual, right? So when you include for instance [First Day]

 

First Day =
CALCULATE (
    SELECTEDVALUE ( dCalendar[Date].[Dia] );
    FILTER ( ALL ( dCalendar ); dCalendar[Date] = MIN ( dCalendar[Date] ) )
)

 

 every row in the table visual has a filter on one day only. Let's take one row as an example, you have:

Year=2018, Month=1, Day=1

so your dCalendar[Date] column is filtered down to that day only, 1st of January, 2018. MIN(dCalendar[Date]) is therefore that day: 2018/01/01. The result of your FILTER( ) operation is dCalendar with only one row, the one containing  2018/01/01.  Then SELECTEDVALUE(... ) returns the day in that only row, which is 1. Bear in mind that you only had one day value to start with. so:

 

 

DAY ( SELECTEDVALUE ( dcalendar[Date] ) )

 would yield exactly the same result as your code.

 

Highlighted
Super User
Super User

Re: Days calculating error

@aBerg

 

 Now on to your next measure:

 

Last Day =
CALCULATE (
    SELECTEDVALUE ( dCalendar[Date].[Dia] );
    FILTER ( ALL ( dCalendar ); dCalendar[Date] = MAX ( dCalendar[Date] ) )
)

 

The only difference with the previous one is that you use MAX( ) instead of MIN( ), so we can follow a very similar logic to determine how it works.  Again, you have a table visual with Year, Month and Day at the beginning, all columns of table dCalendar. All three fields will be creating a filter context for the measures you include in the table visual.  Again, taking the first row as an example, we have

Year=2018, Month=1, Day=1

so your dCalendar[Date] column is filtered down to that day only, 1st of January, 2018.

 

MAX(dCalendar[Date]) is clearly that day, 2018/01/01. Note that no matter what function we use here, MIN( ), MAX( ), AVG()... we'll get 2018/01/01 anyway, since it's the only day available in the filtered dCalendar[Date]. Like before, the result of your FILTER( ) operation is the dCalendar table with only one row, the one containing  2018/01/01 in column dCalendar[Date]. Then SELECTEDVALUE(... ) returns the day in that only row, which is 1.

 

And something else, your code as written would be exactly equivalent to:

 

Last Day =
DAY(MAX ( dCalendar[Date] ) )

 

or am I mistaken? Give it a thought

 

Super User
Super User

Re: Days calculating error

@aBerg

 

OK, this is taking a bit too long.  Let's try and wrap it up 

We can finally build on what we have so far and see what happens with your last measures:

 

Interval Days = ([Last Day]-[First Day])

We've just seen that both [Last Day] and [First Day] return the same number in every row of the table visual, right? So the result of [Interval Days] will be zero. And then, we see why 

 

Calc Target = 
CALCULATE (
    MAXX ( dCalendar; dCalendar[Day] * ( [BaseValue] / [Interval Days] ));
    ALL ( dCalendar[Day] )
)

 

yields infinite. You are diving by zero in every row of the table visual.

 

Now, why doesn't this happen in your card visuals? Because the only filters acting on them with the set-up  that  you have in the file are Year and Month (on the slicers). No filter on Day and therefore MIN(dCalendar[Date]) will actually be the first day in the selected month and MAX(dCalendar[Date]) the last day, as you intended.

 

Conclusion: it all comes down to the filter context the measure is under at time of execution. 

 

Hope this is useful as it took some time to put together Smiley Happy

 

 

 

               

Helpful resources

Announcements
GregDeckler

How to Get Your Question Answered Quickly

Power BI Super User, Greg Deckler, explains

Summit North America

Power Platform Summit North America

Register by September 5 to save $200

Back to School Contest

Back to School Contest

Engage and empower students with Power BI!

MBAS Gallery

Watch Sessions On Demand!

Continue your learning in our online communities.

Top Ideas
Users Online
Currently online: 42 members 945 guests
Please welcome our newest community members: