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

Difference between two dates in two lines using Direct Query

Hi guys.

 

Almost 2 years ago I did this question here and I received a brilliant solution from the colleague @Zubair_Muhammad :

 

DIAS_ENTRE_ESTADOS = 
VAR Previous_Date =
    MINX (
        TOPN (
            1,
            FILTER ( HISTORICO, [ID] = EARLIER ( [ID] ) && [DATA] < EARLIER ( [DATA] ) ),
            [DATA], DESC
        ),
        [DATA]
    )
RETURN
    IF( Previous_Date && NOT [ESTADO] = "01", DATEDIFF ( Previous_Date, [DATA], DAY), 0)

 

I was using "Import" and everything was fine.

Now I'm doing the something, but by a "Direct Query" connection and I'm receiving a strange message:

"EARLIER / EARLIEST references a previous row context that does not exist."

I studied and made research, but I didn't find information about it.

Could anyone help me?

 

Regards.

15 REPLIES 15
amitchandak
Super User
Super User

@Anonymous , if you are viewing by date then try like below. All with date table

current Day =SUM(Sales[Sales Amount])

Last Day Non Continous = CALCULATE(sum('order'[Qty]),filter(all('Date'),'Date'[Date] =MAXX(FILTER(all('Date'),'Date'[Date]<max('Date'[Date])),Table['Date'])))
Day behind Sales = CALCULATE(SUM(Sales[Sales Amount]),dateadd('Date'[Date],-1,Day))

 

Other option

This Day = CALCULATE(sum('order'[Qty]), FILTER(ALL('Date'),'Date'[Date]=max('Date'[Date])))
Last Day = CALCULATE(sum('order'[Qty]), FILTER(ALL('Date'),'Date'[Date]=max('Date'[Date])-1))

 

Refer my blog for direct query : https://community.powerbi.com/t5/Community-Blog/Decoding-Direct-Query-in-Power-BI-Part-1-Time-Intelligence-in/ba-p/922885

 

To get the best of the time intelligence function. Make sure you have a date calendar and it has been marked as the date in model view. Also, join it with the date column of your fact/s. Refer :
https://radacad.com/creating-calendar-table-in-power-bi-using-dax-functions
https://www.archerpoint.com/blog/Posts/creating-date-table-power-bi
https://www.sqlbi.com/articles/creating-a-simple-date-table-in-dax/

See if my webinar on Time Intelligence can help: https://community.powerbi.com/t5/Webinars-and-Video-Gallery/PowerBI-Time-Intelligence-Calendar-WTD-YTD-LYTD-Week-Over-Week/m-p/1051626#M184


Appreciate your Kudos.

 

 

Anonymous
Not applicable

Hi @amitchandak .

 

Thanks for your message.

The situation is: there is a register and this register has status changes.

I need to calculate the time between those changes. One by one.

 

Regards.

Greg_Deckler
Super User
Super User

@Anonymous  EARLIER is not supported in DirectQuery:

https://docs.microsoft.com/en-us/analysis-services/tabular-models/dax-formula-compatibility-in-directquery-mode-ssas-2016?view=asallproducts-allversions

 

Thus, I would do this:

DIAS_ENTRE_ESTADOS = 
VAR CurrentID = [ID]
VAR CurrentData = [DATA]
VAR Previous_Date =
    MINX (
        TOPN (
            1,
            FILTER ( HISTORICO, [ID] = CurrentID && [DATA] < CurrentData ),
            [DATA], DESC
        ),
        [DATA]
    )
RETURN
    IF( Previous_Date && NOT [ESTADO] = "01", DATEDIFF ( Previous_Date, [DATA], DAY), 0)

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

Hi, @Greg_Deckler ,

 

Thanks a lot for your message. I didn't find that doc. 

It didn't work. I received the message: "The value for 'ID' cannot be determined" referring to the line "VAR CurrentID = [ID]".

Then I tried to put the table name, HISTORICO[ID], but it didn't work too.

 

Regards.

 

@Anonymous - Is this a column or a measure? I was just going off of your formula. I cannot fathom why you would be able to use the column references in below formula but not in my formula unless you are trying to create this column in a different table? Hard to tell without having actually seen your data. 

 

DIAS_ENTRE_ESTADOS =
VAR Previous_Date =
MINX (
TOPN (
1,
FILTER ( HISTORICO, [ID] = EARLIER ( [ID] ) && [DATA] < EARLIER ( [DATA] ) ),
[DATA], DESC
),
[DATA]
)
RETURN
IF( Previous_Date && NOT [ESTADO] = "01", DATEDIFF ( Previous_Date, [DATA], DAY), 0)


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

Hi, @Greg_Deckler .

It is a measure.

I'm creating it in the table HISTORICO itself.

But I did another test: I put the "[" and the Power BI Desktop offers to me just model's measures, not a field.

Annotation 2020-05-28 183816.png

When I put the table name, "HISTORICO", it doesn't offer a field.

 

Regards,

 

Emanuel

 

@Anonymous - Right, in measures when you reference a column it must be wrapped in an aggregator like SUM, AVERAGE, MIN, MAX, etc.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

Hi @Greg_Deckler

 

I didn't understand.

I said that I tried your first solution putting in a column and didn't work because MINX is not allowed in Direct Query.

 

DIAS_ENTRE_ESTADOS = 
VAR CurrentID = [ID]
VAR CurrentData = [DATA]
VAR Previous_Date =
    MINX (
        TOPN (
            1,
            FILTER ( HISTORICO, [ID] = CurrentID && [DATA] < CurrentData ),
            [DATA], DESC
        ),
        [DATA]
    )
RETURN
    IF( Previous_Date && NOT [ESTADO] = "01", DATEDIFF ( Previous_Date, [DATA], DAY), 0)

 

Regards,

MINX is supported in measures. https://docs.microsoft.com/en-us/analysis-services/tabular-models/dax-formula-compatibility-in-direc...

 

What I am saying, is if this is a measure you need to wrap column aggregations in an aggregator:

DIAS_ENTRE_ESTADOS = 
VAR CurrentID = MAX([ID])
VAR CurrentData = MAX([DATA])
VAR Previous_Date =
    MINX (
        TOPN (
            1,
            FILTER ( HISTORICO, [ID] = CurrentID && [DATA] < CurrentData ),
            [DATA], DESC
        ),
        [DATA]
    )
RETURN
    IF( Previous_Date && NOT [ESTADO] = "01", DATEDIFF ( Previous_Date, [DATA], DAY), 0)

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

I knew it, but I said that in my original model was a column, not a measure (was my fault).

But as a measure, it didn't work too, as I told before.

So, the issue persists.

 

Regards.

@Anonymous Well if it is a measure, just do this:

DIAS_ENTRE_ESTADOS = 
VAR CurrentID = MAX([ID])
VAR CurrentData = MAX([DATA])
VAR Previous_Date =
    MINX (
        TOPN (
            1,
            FILTER ( HISTORICO, [ID] = CurrentID && [DATA] < CurrentData ),
            [DATA], DESC
        ),
        [DATA]
    )
RETURN
    IF( Previous_Date && NOT [ESTADO] = "01", DATEDIFF ( Previous_Date, [DATA], DAY), 0)

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

Hi @Greg_Deckler!

Thanks a lot for your help.

Some information:

1. I tried to use this solution, but it returns an error on the last line:

"The value for 'DATA' cannot be determined. Either the column doesn't exist, or there is no current row for this column."

2. I checked again the old model, based on "Import", and I realized that I didn't create a column, but a measure instead of. Sorry.

Then, I tried your first solution again in a column, but I received a message saying that I can't use MINX on Direct Query.

I'm doing research, and I'm starting to believe that I can't do it using DAX on Direct Query.

On the other hand, I've read that EARLY just can be used on measures.

Complicated.

 

@Anonymous , Can you share sample data and sample output in a table format?

I will try to load to SQL server and try in direct query mode.

Thinking if Rank can help us to get the last record

Anonymous
Not applicable

Hi @amitchandak 

 

Thanks for your help.

Follow a link to a sample.

If you want to take a look at it too, @Greg_Deckler , please be my guess.

For security reasons, I'll need to delete it in 24 hours.

 

https://1drv.ms/u/s!Alj4o2gZuCvog9sxJwEZ5g56FeDmyg?e=U25u2z

 

Regards.

@Anonymous , I downloaded. In case I did not back with the solution. Mark me again on topic update

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.