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

Need help with power query or DAX formula

Hi,

I have 4 columns in a table.

First Column is a unique ID, Second column is the ID (same as first column but has multiple entries),

thrid column is the status and fourth column is date.

I need to take the unique ID, compare and filter the second column. If there are multiple values then I need the status of the latest date and print it in a new fifth column.

UniqueIDIDSTATUSDATE
123456123456ACTIVE30-Jun-21
344566564344566564ACTIVE30-Jun-21
242423423242423423ACTIVE30-Jun-21
2323223232ACTIVE30-Jun-21
2232323222323232ACTIVE30-Jun-21
2323232323232323ACTIVE30-Jun-21
 123456INACTIVE30-Jul-21
 344566564ACTIVE31-Jul-21
 242423423INACTIVE15-Aug-21
 23232ACTIVE15-Aug-21

Can you please help me to achieve the result. Thank you

1 ACCEPTED SOLUTION
v-yiruan-msft
Community Support
Community Support

Hi @manipepsi ,

Please try to deal with it in Power Query Editor with the below applied codes and check whether you still get the errpr message with "there is no enough memory"... You can find the details in the attachment.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyNjE1U9JBMBydQzzDXIEMYwNdr9I8XSNDpVidaCVjE6C0mamZCUgGiY1DuZEJEBoDEVAGmY1LuTEQgpRCaVzKwPIQlQgmPjONIS5AMHEoRg4BTz9URTlIirB73RBNFbKPkUwzNNV1LE1HVofuAyQVsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [UniqueID = _t, ID = _t, STATUS = _t, DATE = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"UniqueID", Int64.Type}, {"ID", Int64.Type}, {"STATUS", type text}, {"DATE", type date}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"ID"}, {{"Latest date", each List.Max([DATE]), type nullable date}, {"Details", each _, type table [UniqueID=nullable number, ID=nullable number, STATUS=nullable text, DATE=nullable date]}}),
    #"Expanded Details" = Table.ExpandTableColumn(#"Grouped Rows", "Details", {"UniqueID", "STATUS", "DATE"}, {"UniqueID", "STATUS", "DATE"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Details", "Latest Status", each if [DATE]=[Latest date] then [STATUS] else null),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Latest date"})
in
    #"Removed Columns"

The detailed steps as below:

1. Get the latest date group by ID and expand the details

yingyinr_1-1629706225490.png

2. Add conditional column to get the status with latest date

yingyinr_2-1629706454135.png

3. Remove the latest date column

yingyinr_0-1629705994260.png

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

6 REPLIES 6
v-yiruan-msft
Community Support
Community Support

Hi @manipepsi ,

Please try to deal with it in Power Query Editor with the below applied codes and check whether you still get the errpr message with "there is no enough memory"... You can find the details in the attachment.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyNjE1U9JBMBydQzzDXIEMYwNdr9I8XSNDpVidaCVjE6C0mamZCUgGiY1DuZEJEBoDEVAGmY1LuTEQgpRCaVzKwPIQlQgmPjONIS5AMHEoRg4BTz9URTlIirB73RBNFbKPkUwzNNV1LE1HVofuAyQVsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [UniqueID = _t, ID = _t, STATUS = _t, DATE = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"UniqueID", Int64.Type}, {"ID", Int64.Type}, {"STATUS", type text}, {"DATE", type date}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"ID"}, {{"Latest date", each List.Max([DATE]), type nullable date}, {"Details", each _, type table [UniqueID=nullable number, ID=nullable number, STATUS=nullable text, DATE=nullable date]}}),
    #"Expanded Details" = Table.ExpandTableColumn(#"Grouped Rows", "Details", {"UniqueID", "STATUS", "DATE"}, {"UniqueID", "STATUS", "DATE"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Details", "Latest Status", each if [DATE]=[Latest date] then [STATUS] else null),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Latest date"})
in
    #"Removed Columns"

The detailed steps as below:

1. Get the latest date group by ID and expand the details

yingyinr_1-1629706225490.png

2. Add conditional column to get the status with latest date

yingyinr_2-1629706454135.png

3. Remove the latest date column

yingyinr_0-1629705994260.png

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi @v-yiruan-msft 

Thanks for your help.

Your solution fullfilled my need.

 

Thank you!

manipepsi
Frequent Visitor

Thanks @VahidDM for the quick reply.

I have still 2 questions:

1) In the "Latest Status" whether it is possible to have the latest status (Inactive, active etc.) only for "Unique ID" ?

Meaning: What I need is the latest status for every Unique ID so that I can filter the latest statuses which I need.

In this solution I cannot able to filter 2 statuses and get the unique IDs of those.

2) Whether it is possible to have "Latest Status" as a column and not as a measure?

 

Can you please also help with these questions?

 

Thanks in advance!!

Hi @manipepsi 

Yes, It is.

Add a new column and try the below measure:

Latest status = 
VAR _ID = 'Table'[UniqueID]
    
RETURN
    VAR _Maxdate =
        CALCULATE (
            MAX ( 'Table'[DATE] ),
            FILTER ( ALLEXCEPT ( 'Table', 'Table'[ID] ), MAX ( 'Table'[ID] ) )
        )
    RETURN
        CALCULATE (
            FIRSTNONBLANK ( 'Table'[STATUS], "" ),
            FILTER ( ALL ( 'Table' ), 'Table'[ID] = _ID && 'Table'[DATE] = _Maxdate )
        )

The output will be as below:

 

VahidDM_0-1629414932297.png

 

Did I answer your question? Mark my post as a solution!

Appreciate your Kudos VahidDM_1-1629414941603.png !!

HI @VahidDM 

Thanks for your quick response.

I tried your solution but I got the error "There is no enough memory to complete this operation" .

I have 16GB of RAM but my data has 300K rows.

Whether there is any possibility to have a memory efficient solution?

 

Thank you

VahidDM
Super User
Super User

Hi @manipepsi 

 

Please try this measure:

Latest status = 
VAR _ID =
    IF (
        ISBLANK ( MAX ( 'Table'[UniqueID] ) ),
        MAX ( 'Table'[ID] ),
        MAX ( 'Table'[UniqueID] )
    )
RETURN
    VAR _Maxdate =
        CALCULATE (
            MAX ( 'Table'[DATE] ),
            FILTER ( ALLEXCEPT ( 'Table', 'Table'[ID] ), MAX ( 'Table'[ID] ) )
        )
    RETURN
        CALCULATE (
            FIRSTNONBLANK ( 'Table'[STATUS], "" ),
            FILTER ( ALL ( 'Table' ), 'Table'[ID] = _ID && 'Table'[DATE] = _Maxdate )
        )

the output will be as below:

VahidDM_0-1629335062916.png

Did I answer your question? Mark my post as a solution!

Appreciate your Kudos VahidDM_1-1629335081623.png !!

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.