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
rbreneman
Helper II
Helper II

Combining Rows using DAX

Hi,

 

I have a table that contains census data, I've already added DAX columns to this table to return a true/false on whether this is the start date or end date of a stay.

 

I'm using DAX to create a new table and with UNION I've pulled the start and end date rows in, but I need a way to combine these rows so that I end up with a single row containing RoomID, BedID, ResidentID, StartDate, and EndDate values. Screenshot below illustrates what I currently have. This needs to occur with DAX as the data in the other table is already using DAX to determine the start and end dates so the neccessary logic wouldn't available to me from query editor. Maybe UNION isn't the best way to approach this, if so, I'm open to anything that works!

 

Thanks so much!

Ryan

 

CombineRows.png

1 ACCEPTED SOLUTION
AlB
Super User
Super User

Hi @rbreneman 

I don't think you need the UNION. You could just do (Table1 is your base table "view_ods...")

 

NewTable =
ADDCOLUMNS (
    SUMMARIZE ( Table1, Table1[RoomID], Table1[BedID], Table1[ResidentID] ),
    "StartDate", CALCULATE ( MIN ( Table1[CensusDate] ) ),
    "EndDate", CALCULATE ( MAX ( Table1[CensusDate] ) )
)

 

where we are assuming the StartDate is the earliest on and the EndDate is the latest one. If you do need the check you use in your code:

 

NewTable =
ADDCOLUMNS (
    SUMMARIZE ( Table1, Table1[RoomID], Table1[BedID], Table1[ResidentID] ),
    "StartDate", CALCULATE ( DISTINCT ( Table1[CensusDate] ), Table1[IsEarliestByDate] = TRUE () ),
    "EndDate", CALCULATE ( DISTINCT ( Table1[CensusDate] ), Table1[IsLatestByDate] = TRUE () )
)

 

 

Please mark the question solved when done and consider giving kudos if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

 

SU18_powerbi_badge

View solution in original post

2 REPLIES 2
AlB
Super User
Super User

Hi @rbreneman 

I don't think you need the UNION. You could just do (Table1 is your base table "view_ods...")

 

NewTable =
ADDCOLUMNS (
    SUMMARIZE ( Table1, Table1[RoomID], Table1[BedID], Table1[ResidentID] ),
    "StartDate", CALCULATE ( MIN ( Table1[CensusDate] ) ),
    "EndDate", CALCULATE ( MAX ( Table1[CensusDate] ) )
)

 

where we are assuming the StartDate is the earliest on and the EndDate is the latest one. If you do need the check you use in your code:

 

NewTable =
ADDCOLUMNS (
    SUMMARIZE ( Table1, Table1[RoomID], Table1[BedID], Table1[ResidentID] ),
    "StartDate", CALCULATE ( DISTINCT ( Table1[CensusDate] ), Table1[IsEarliestByDate] = TRUE () ),
    "EndDate", CALCULATE ( DISTINCT ( Table1[CensusDate] ), Table1[IsLatestByDate] = TRUE () )
)

 

 

Please mark the question solved when done and consider giving kudos if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

 

SU18_powerbi_badge

@AlB Thank you so much for the quick reply!! This is perfect. I went with the 2nd example you provided as there is some logic happening on the other table beyond just the date. Thanks again!

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