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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
jcastr02
Post Prodigy
Post Prodigy

Only show last 2 date entries - pq or DAX

Hello,
I am trying to achieve something in Power Query or if there is a way in DAX, that's okay too.  Each store # has three different types of support (DE, DR, PCC).  I have a running log start date/end date for each.  Some within the same support may run concurrently.  I am trying to just show the latest two entries.  However, if the end date is blank  (it means it's still active, so I'd like to show that one).  In the below example, see the current log on left, and desired result on right. 

StoreSupport TypeStart DateEnd Date StoreSupport TypeStart DateEnd Date
211DE9/25/2023  211DE9/25/2023 
211DE8/11/20208/20/2020 211DE8/21/20209/20/2023
211DE5/2/20198/10/2020 211DR3/6/2017 
211DE8/21/20209/20/2023 211DR6/9/20209/20/2023
211DE3/6/201710/4/2018 211PCC7/1/20237/31/2023
211DR3/6/2017  211PCC2/1/20243/31/2024
211DR8/11/20208/20/2020 215DE12/1/2022 
211DR6/9/20209/20/2023 215DE3/2/20217/19/2021
211DR9/5/20196/3/2020 215DR12/1/2022 
211PCC7/1/20237/31/2023 215DR8/24/202312/13/2023
211PCC2/1/20232/28/2023 215PCC2/1/20232/28/2023
211PCC2/1/20243/31/2024 215PCC7/1/20237/31/2023
215DE9/7/201710/4/2018     
215DE12/1/2022      
215DE4/10/20209/2/2020     
215DE3/2/20217/19/2021     
215DR4/10/20207/19/2021     
215DR9/7/20179/2/2020     
215DR12/1/2022      
215DR8/24/202312/13/2023     
215PCC2/1/20232/28/2023     
215PCC7/1/20237/31/2023     
1 ACCEPTED SOLUTION
dufoq3
Super User
Super User

Hi @jcastr02,

 

Result

dufoq3_0-1721312345802.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZFBDoQgDEWvYlib1LYgsnZmP2FrvP81plBFJOrMhvzQx6f9XRZDiKY3r7ccAcgBDcSiO7P2p+IEiKk4ZE2D6gaS53KPQflrRh4Xo7AZcQsxjMnIixQbm/RUM/HMdG3tZ7cJGiE8NhJzwe0TjcCtzWee5fSAe2weGFsfheiAJKPpEbJ5Ot60Qu7Ykr+LpjC4GVGVTSnaspk8dz1TYVjvUacLqismNj63UNXt1V/xqVldJNk9twRyHZz7J90Dut7T+gU=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Store = _t, #"Support Type" = _t, #"Start Date" = _t, #"End Date" = _t]),
    ChangedTypeUS = Table.TransformColumnTypes(Source,{{"Start Date", type date}, {"End Date", type date}}, "en-US"),
    DuplicatedColumn = Table.DuplicateColumn(ChangedTypeUS, "End Date", "End Date Copy"),
    ReplacedValue = Table.ReplaceValue(DuplicatedColumn,null, Date.From(DateTime.LocalNow()),Replacer.ReplaceValue,{"End Date Copy"}),
    GroupedRows = Table.Group(ReplacedValue, {"Store", "Support Type"}, {{"All", each Table.RemoveColumns(Table.MaxN(_, {"End Date Copy"}, 2), {"End Date Copy"}), type table}}),
    Combined = Table.Combine(GroupedRows[All])
in
    Combined

Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

View solution in original post

3 REPLIES 3
dufoq3
Super User
Super User

Hi @jcastr02,

 

Result

dufoq3_0-1721312345802.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZFBDoQgDEWvYlib1LYgsnZmP2FrvP81plBFJOrMhvzQx6f9XRZDiKY3r7ccAcgBDcSiO7P2p+IEiKk4ZE2D6gaS53KPQflrRh4Xo7AZcQsxjMnIixQbm/RUM/HMdG3tZ7cJGiE8NhJzwe0TjcCtzWee5fSAe2weGFsfheiAJKPpEbJ5Ot60Qu7Ykr+LpjC4GVGVTSnaspk8dz1TYVjvUacLqismNj63UNXt1V/xqVldJNk9twRyHZz7J90Dut7T+gU=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Store = _t, #"Support Type" = _t, #"Start Date" = _t, #"End Date" = _t]),
    ChangedTypeUS = Table.TransformColumnTypes(Source,{{"Start Date", type date}, {"End Date", type date}}, "en-US"),
    DuplicatedColumn = Table.DuplicateColumn(ChangedTypeUS, "End Date", "End Date Copy"),
    ReplacedValue = Table.ReplaceValue(DuplicatedColumn,null, Date.From(DateTime.LocalNow()),Replacer.ReplaceValue,{"End Date Copy"}),
    GroupedRows = Table.Group(ReplacedValue, {"Store", "Support Type"}, {{"All", each Table.RemoveColumns(Table.MaxN(_, {"End Date Copy"}, 2), {"End Date Copy"}), type table}}),
    Combined = Table.Combine(GroupedRows[All])
in
    Combined

Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Thank you so much @dufoq3  would you happen to have the sample BI that you can attach here?

You're welcome. It is not necessary to attach pbix file. You can use my query. If you don't know how - read note below my post.


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.