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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Erwin
Helper II
Helper II

Calculating age of document dynamically

Hi all,

 

I've got two tables, ARHistory and DocumentDates, the first being my fact table and the second being my calendar table. These tables are related through 'ARHistory'[Document date] -> 'DocumentDates'[DateKey].

 

I want to to write a measure [AgeOfDocument], which should calculate the difference between 'ARHistory'[Due date] and 'DocumentDates'[DateKey]. This is what I got so far:

 

AgeOfDocument:=
CALCULATE (
    COUNTROWS ( DocumentDates ),
    ALL ( DocumentDates[DateKey] ),
    DATESBETWEEN (
        DocumentDates[DateKey],
        MAX ( ARHistory[Due date] ),
        MAX ( DocumentDates[DateKey] )
    )
)

 

Obviously [AgeOfDocument] should give me the number of days between the selected date from 'DocumentDates'[DateKey] and each value of 'ARHistory'[Due date]. Unfortunately the above formula is giving me a fixed number of days for each 'ARHistory'[Due date].

 

Can you help me with this one?

 

Many thanks in advance!

 

Rg. Erwin

 

 

 

 

1 ACCEPTED SOLUTION

I think you need to get rid of your relationship or have a separate calendar table and do this:

 

DaysAging = (MAX([DueDate]) - MAX('Calendar'[Date])) * 1.

This was a disconnected calendar table and an enter data query like this:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Tc4xC8IwEAXgv3JkLrRNLc7Swak4GHQIHc54bYJnAmnAv29okDoe7+O901rcMQL6J4whEuRDVKKvW1nLpj2KqdLiMsPoDG1o4GBeayH9Tk6gkAnCDOoT4PpGZrg5ZlyoYNn84wGTsRTBeUg2j1rCVNhhZyonZ+cXio9ImP9Dv5nuVzV9AQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Book = _t, DueDate = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Book", type text}, {"DueDate", type date}})
in
    #"Changed Type"

And:

 

books.png

 

 

 

 



Follow on LinkedIn
@ 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!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

5 REPLIES 5
Greg_Deckler
Community Champion
Community Champion

Just create a column or measure like:

 

AgeOfDocument = ARHistory[Due date] - ARHistory[Document date] * 1.


Follow on LinkedIn
@ 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!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

I'm sorry, but that's not going to do it. I need this measure to evaluate each individual value in 'ARHistory'[Due date] against the selected 'DocumentDates'[DateKey]. A calculated column will not do this as it will evaluate against a one (fixed) value within 'DocumentDates'[DateKey]. Also, using your proposed formula as a measure will not work, as it gives ambiguous results.

 

My idea was to count the rows for a set of dates, filtered by the DATESBETWEEN function. Unfortunately, somehow the above formula evaluates only on 'DocumentDates'. The result is always the row count of the MAX of 'DocumentDates'[DateKey] and 'DocumentDates'[DateKey].

 

Rg. Erwin

OK, so let me parrot this back to make sure I understand. You want to select some date out of the calendar in a slicer or something, like:

 

May 24th, 2017

 

Then you want to have a measure that calculates the days aging for books with due dates like:

 

May 12th, 2017

May 15th, 2017

May 20th, 2017

May 24th, 2017

May 30th, 2017

 

The expected results would be:

 

-12

-9

-4

0

6



Follow on LinkedIn
@ 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!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Exactly!! And if I select another date in the slicer, for example:

 

May 20th, 2017

 

I want the same measure to calculate the days aging as:

 

-8

-5

0

4

10

 

Rg. Erwin

I think you need to get rid of your relationship or have a separate calendar table and do this:

 

DaysAging = (MAX([DueDate]) - MAX('Calendar'[Date])) * 1.

This was a disconnected calendar table and an enter data query like this:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Tc4xC8IwEAXgv3JkLrRNLc7Swak4GHQIHc54bYJnAmnAv29okDoe7+O901rcMQL6J4whEuRDVKKvW1nLpj2KqdLiMsPoDG1o4GBeayH9Tk6gkAnCDOoT4PpGZrg5ZlyoYNn84wGTsRTBeUg2j1rCVNhhZyonZ+cXio9ImP9Dv5nuVzV9AQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Book = _t, DueDate = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Book", type text}, {"DueDate", type date}})
in
    #"Changed Type"

And:

 

books.png

 

 

 

 



Follow on LinkedIn
@ 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!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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

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.