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

Filter Rows based on validity date within group

Hi all,

 

I have a bit of a problem that I cannot fix by myself. So hopefully this forum can help me with that.

 

My goal ist to create a monthly database with loan contracts. These loan contracts have securities which are maintained in a differen

td data table. This data tables looks as such:

 

Contract IDType of SecurityLocation of Securityvalid until
1CheckSafe1.1.2022
1CheckSafe1.1.2025
1Mortgage 1.1.2026
2GuarantorCopy1.1.2020
2CheckSafe1.1.2022
3GuarantorCopy1.1.2020
3GuarantorCopy1.1.2030

 

My contract table contains one Contract ID per row with several financial and contractual fields. What I would like to do now is map the corresponding security to the contract table BUT ONLY the security that is valid at the reporting date.

 

For example:

I am creating a report for Nov 2024. The corresponding securities should be valid at reporting date, i.e. at Nov. 31st 2024, or the latest security available for the contract ID. 

Contract IDStartEndAmountType of SecurityLocation of Security
1.........CheckSafe
2.........Check Safe
3.........GuarantorCopy

 

How would I go about this? 
Does it make sense to group and shorten the security data table to just single contract ID at the given report date and then map it to the contract table?

 

Thank you very much for your help!

2 ACCEPTED SOLUTIONS
jgeddes
Super User
Super User

You can filter your security table to dates greater than or equal to the report date as you mentioned and then merge it into the contract table using the 'Contract ID' and 'Type of Security' as the key in both tables. 

Note that this method will not return any values for contract 2 as the security is only valid to 2022 in your example data.





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

Proud to be a Super User!





View solution in original post

Anonymous
Not applicable

Hi @SevanT ,
Thanks for jgeddes reply.

You can try the following code to filter safe table

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXLOSE3OBtLBiWmpQMpQz1DPyMDISClWB5+8KVzeN7+oJD0xHSSngJA3A8sbAQXcSxOLEvNK8otAZuUXVCLUGMDV4HODMRFm4FVjDFQTCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Contract ID" = _t, #"Type of Security" = _t, #"Location of Security" = _t, #"valid until" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Contract ID", Int64.Type}, {"Type of Security", type text}, {"Location of Security", type text}, {"valid until", type date}}),
    FilteredRows = Table.SelectRows(#"Changed Type", each [valid until] > #date(2024, 11, 30)),
    FilteredCheckSafe = Table.SelectRows(FilteredRows, each [Type of Security] = "Check" and [Location of Security] = "Safe"),
    NonFilteredRows = Table.SelectRows(#"Changed Type", each [valid until] <= #date(2024, 11, 30)),
    GroupedRows = Table.Group(NonFilteredRows, {"Contract ID"}, {{"MaxDateRow", each Table.Max(_, "valid until")}}),
    ExpandedRows = Table.ExpandRecordColumn(GroupedRows, "MaxDateRow", { "Type of Security", "Location of Security", "valid until"}),
    FilteredCheckSafeIDs = Table.SelectColumns(FilteredCheckSafe, {"Contract ID"}),
    FilteredExpandedRows = Table.SelectRows(ExpandedRows, each not List.Contains(FilteredCheckSafeIDs[Contract ID], [Contract ID])),
    Result = Table.Combine({FilteredCheckSafe, FilteredExpandedRows})
in
    Result

vheqmsft_0-1733972611126.png

Then merge the security table with the contract table based on the Contract ID

 

Best regards,
Albert He


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

3 REPLIES 3
Omid_Motamedise
Super User
Super User

Initiilly filter the td data table for the validity after november then use merge to combine the tables

If my answer helped solve your issue, please consider marking it as the accepted solution. It helps others in the community find answers faster—and keeps the community growing stronger!
You can also check out my YouTube channel for tutorials, tips, and real-world solutions in Power Query with the following link
https://youtube.com/@omidbi?si=96Bo-ZsSwOx0Z36h
Anonymous
Not applicable

Hi @SevanT ,
Thanks for jgeddes reply.

You can try the following code to filter safe table

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXLOSE3OBtLBiWmpQMpQz1DPyMDISClWB5+8KVzeN7+oJD0xHSSngJA3A8sbAQXcSxOLEvNK8otAZuUXVCLUGMDV4HODMRFm4FVjDFQTCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Contract ID" = _t, #"Type of Security" = _t, #"Location of Security" = _t, #"valid until" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Contract ID", Int64.Type}, {"Type of Security", type text}, {"Location of Security", type text}, {"valid until", type date}}),
    FilteredRows = Table.SelectRows(#"Changed Type", each [valid until] > #date(2024, 11, 30)),
    FilteredCheckSafe = Table.SelectRows(FilteredRows, each [Type of Security] = "Check" and [Location of Security] = "Safe"),
    NonFilteredRows = Table.SelectRows(#"Changed Type", each [valid until] <= #date(2024, 11, 30)),
    GroupedRows = Table.Group(NonFilteredRows, {"Contract ID"}, {{"MaxDateRow", each Table.Max(_, "valid until")}}),
    ExpandedRows = Table.ExpandRecordColumn(GroupedRows, "MaxDateRow", { "Type of Security", "Location of Security", "valid until"}),
    FilteredCheckSafeIDs = Table.SelectColumns(FilteredCheckSafe, {"Contract ID"}),
    FilteredExpandedRows = Table.SelectRows(ExpandedRows, each not List.Contains(FilteredCheckSafeIDs[Contract ID], [Contract ID])),
    Result = Table.Combine({FilteredCheckSafe, FilteredExpandedRows})
in
    Result

vheqmsft_0-1733972611126.png

Then merge the security table with the contract table based on the Contract ID

 

Best regards,
Albert He


If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

 

jgeddes
Super User
Super User

You can filter your security table to dates greater than or equal to the report date as you mentioned and then merge it into the contract table using the 'Contract ID' and 'Type of Security' as the key in both tables. 

Note that this method will not return any values for contract 2 as the security is only valid to 2022 in your example data.





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

Proud to be a Super User!





Helpful resources

Announcements
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.

June 2025 community update carousel

Fabric Community Update - June 2025

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