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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Thomvdw
Frequent Visitor

New table with filtered distinct ID's

Hi Community,

 

I have a datatable with multiple months below each other. The datatable consists of the column 'period' and 'ContractID'. 

 

The goal is to create a new table with only the distinct ContractID's from the last available month.

 

I tried multiple variations of lines of code, but none gave me the right answer. 

 

Hopefully somebody here can help.

 

Thanks, Thom

1 ACCEPTED SOLUTION

Hi Thomvdw,
 
 
New Table =
VAR _MaxPeriod = MAX('Table'[Period])
RETURN
CALCULATETABLE(
SUMMARIZE('Table', 'Table'[Period], 'Table'[ContractID]),
'Table'[Period ] = _MaxPeriod
)

View solution in original post

4 REPLIES 4
Anonymous
Not applicable

1. Sample, representative data for the problem.

2. Transformation description.

3. Desired outcome.

Hi Daxer,

 

Sample datatable is:

Period ContractID
2021-01 1
2021-01 2
2021-01 3
2021-01 3
2021-02 2
2021-02 3
2021-02 3
2021-02 4


In this sample the last month is 2021-02, so I expect the new datatable outcome to be:

Period ContractID
2021-02 2
2021-02 3
2021-02 4


Hope this clarifies

Hi Thomvdw,
 
 
New Table =
VAR _MaxPeriod = MAX('Table'[Period])
RETURN
CALCULATETABLE(
SUMMARIZE('Table', 'Table'[Period], 'Table'[ContractID]),
'Table'[Period ] = _MaxPeriod
)
Anonymous
Not applicable

// Data
let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtQ1MFQwVIrVQfCMUHjGOHlGKCqN0ORQeSZKsbEA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Period ContractID" = _t]),
    #"Split Column by Delimiter" = Table.SplitColumn(Source, "Period ContractID", Splitter.SplitTextByEachDelimiter({" "}, QuoteStyle.Csv, false), {"Period ContractID.1", "Period ContractID.2"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Period ContractID.1", type date}, {"Period ContractID.2", Int64.Type}}),
    #"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"Period ContractID.1", "Period"}, {"Period ContractID.2", "ContractID"}}),
    #"Removed Duplicates" = Table.Distinct(#"Renamed Columns"),
    #"Calculate Max Date" = List.Max(#"Removed Duplicates"[Period], null),
    #"Should Keep" = Table.AddColumn(
        #"Removed Duplicates", "Should Keep?",
        each [Period] = #"Calculate Max Date"
    ),
    #"Filtered Rows" = Table.SelectRows(#"Should Keep", each ([#"Should Keep?"] = true)),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Should Keep?"})
in
    #"Removed Columns"

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.