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
mwmckee
Frequent Visitor

DAX to calculate days elapsed from history table

Greetings!

 

I am presented with a table containing history of residential units with each record representing when a tenant moves in and subsequently moves out.  A sample extract with one unit only is below.

Unit Move in-out history.jpg

I would like to generate a report that shows the vacant days for each unit as follows.

Days Vacant.png

 

Can this be addressed using DAX?  

 

I have also been considering using M to create a summary table (somehow) with a row for each day of the year and each unit with a simple column of Vacant (0/1) which would simplify summations.  Is this a better approach?

 

Thank you in advance for any insight or ideas.

 

Michael

1 ACCEPTED SOLUTION
Phil_Seamark
Employee
Employee

Hi @mwmckee

 

Here is a way you can create a new calculated table in DAX that has a good crack at working out the gaps inbetween.

 

Not sure how you plan to allocate dates empty when that period spans two years, but this is hopefully a good start.

 

New Table = 
VAR StartingPoint = 
    ADDCOLUMNS(
        'Table1',
        "Guaranteed Ranking", CALCULATE(
            COUNTROWS('Table1'),
            FILTER(ALL('Table1'),
            'Table1'[MoveIn] < EARLIER('Table1'[MoveIn]) &&
            'Table1'[UnitID] = EARLIER('Table1'[UnitID])
            )
            )+1)
VAR JoinTable = SELECTCOLUMNS(
                    StartingPoint,
                    "UnitID-j",[UnitID],
                    "MoveIn-j",[MoveIn],
                    "Rank-j",[Guaranteed Ranking]-1
                    )          
VAR JoinedTable =  
            FILTER(
                CROSSJOIN(StartingPoint,JoinTable),
                [UnitID] = [UnitID-j] && 
                [Guaranteed Ranking] = [Rank-j]
                )
RETURN SELECTCOLUMNS(
            JoinedTable ,
            "UnitID" , [UnitID] ,
            "MoveOut" , [MoveOut] ,
            "MoveInt" , [MoveIn-j] ,
            "Dates Empty" , DATEDIFF([MoveOut] ,[MoveIn-j],DAY) ,
            "Year" , YEAR('Table1'[MoveIn])
            )

To learn more about DAX visit : aka.ms/practicalDAX

Proud to be a Datanaut!

View solution in original post

2 REPLIES 2
Phil_Seamark
Employee
Employee

Hi @mwmckee

 

Here is a way you can create a new calculated table in DAX that has a good crack at working out the gaps inbetween.

 

Not sure how you plan to allocate dates empty when that period spans two years, but this is hopefully a good start.

 

New Table = 
VAR StartingPoint = 
    ADDCOLUMNS(
        'Table1',
        "Guaranteed Ranking", CALCULATE(
            COUNTROWS('Table1'),
            FILTER(ALL('Table1'),
            'Table1'[MoveIn] < EARLIER('Table1'[MoveIn]) &&
            'Table1'[UnitID] = EARLIER('Table1'[UnitID])
            )
            )+1)
VAR JoinTable = SELECTCOLUMNS(
                    StartingPoint,
                    "UnitID-j",[UnitID],
                    "MoveIn-j",[MoveIn],
                    "Rank-j",[Guaranteed Ranking]-1
                    )          
VAR JoinedTable =  
            FILTER(
                CROSSJOIN(StartingPoint,JoinTable),
                [UnitID] = [UnitID-j] && 
                [Guaranteed Ranking] = [Rank-j]
                )
RETURN SELECTCOLUMNS(
            JoinedTable ,
            "UnitID" , [UnitID] ,
            "MoveOut" , [MoveOut] ,
            "MoveInt" , [MoveIn-j] ,
            "Dates Empty" , DATEDIFF([MoveOut] ,[MoveIn-j],DAY) ,
            "Year" , YEAR('Table1'[MoveIn])
            )

To learn more about DAX visit : aka.ms/practicalDAX

Proud to be a Datanaut!

This is a great start Phil, not just a good one.

 

Thank you!

Michael

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.

Top Solution Authors