Forum Discussion

adhossain's avatar
adhossain
Frequent Visitor
3 years ago
Solved

Dynamically filter duplicates

I ran into a strange situation where I have an excel table like below;

 

ActivityUnique ID of Activity# AchievedReporting MonthStatus
Training 1ID01202023-01Ongoing
Orientation 1ID02262023-01Ongoing
Distribution 1ID03252023-01Ongoing
Training 1ID01212023-02Ongoing
Orientation 1ID02692023-02Ongoing
Distribution 1ID03302023-02Ongoing
Training 1ID01332023-03Completed
Orientation 1ID02832023-03Ongoing
Distribution 1ID03742023-03Ongoing
Training 2ID04392023-04Ongoing
Orientation 1ID02252023-04Completed
Distribution 1ID03962023-04Ongoing
Training 2ID04332023-05Ongoing
Orientation 2ID05412023-05Ongoing
Distribution 1ID03822023-05Completed
Training 2ID04722023-06Ongoing
Orientation 2ID05362023-06Completed
Distribution 2ID06852023-06Ongoing

 

I am able to remove duplicates based on "Unique ID of Activity" column where I want to keep the items which are "Completed" only but remove the "Ongoing" duplicate entries. I used "Table.Buffer" method. My query M code is below;

 

 

 

 

 

 

let
    Source = Excel.Workbook(File.Contents("C:\test\Activity_Tracker.xlsx"), null, true),
    Acticity_Table_Table = Source{[Item="Acticity_Table",Kind="Table"]}[Data],
    #"Changed Type" = Table.TransformColumnTypes(Acticity_Table_Table,{{"Activity", type text}, {"Unique ID of Activity", type text}, {"# Achieved", Int64.Type}, {"Reporting Month", type date}, {"Status", type text}}),
    #"Sorted Rows" = Table.Buffer(Table.Sort(#"Changed Type",{{"Status", Order.Ascending}})),
    #"Removed Duplicates" = Table.Distinct(#"Sorted Rows", {"Unique ID of Activity"})
in
    #"Removed Duplicates"

 

 

 

 

 

 

This all works fine and returns a similar table like below which is very obvious to happen.

 

But I want a differnt result. As you can see, though my dataset starts from 2023-01 (or January 2023 in reporting Month column), I am not able to see what happened during that time as "Remove Duplicates" command removed those rows. How can I be able to see what happened in first 1 month, or first two months or first three months.

 

What could be a solution to this scenario?

  • Hi adhossain ,

    I don't think you want to remove duplicates in Power Query if later you want to be able to visualize those rows in your matrix.

    I would load everything instead and control what is shown, through measures, like this:

    Measures:

    _# Achieved = 
    VAR
    maxReportingMonth = CALCULATE(MAX('Table'[Reporting Month]),ALLSELECTED('Table'[Reporting Month]))
    RETURN
    CALCULATE(SELECTEDVALUE('Table'[# Achieved]),'Table'[Reporting Month]= maxReportingMonth)
    --------------------
    _# Reporting Month = 
    VAR
    maxReportingMonth = CALCULATE(MAX('Table'[Reporting Month]),ALLSELECTED('Table'[Reporting Month]))
    RETURN
    maxReportingMonth
    --------------------
    _Status = 
    VAR
    maxReportingMonth = CALCULATE(MAX('Table'[Reporting Month]),ALLSELECTED('Table'[Reporting Month]))
    RETURN
    CALCULATE(SELECTEDVALUE('Table'[Status]),'Table'[Reporting Month]= maxReportingMonth)

     

5 Replies

  • Hi adhossain ,

    I don't think you want to remove duplicates in Power Query if later you want to be able to visualize those rows in your matrix.

    I would load everything instead and control what is shown, through measures, like this:

    Measures:

    _# Achieved = 
    VAR
    maxReportingMonth = CALCULATE(MAX('Table'[Reporting Month]),ALLSELECTED('Table'[Reporting Month]))
    RETURN
    CALCULATE(SELECTEDVALUE('Table'[# Achieved]),'Table'[Reporting Month]= maxReportingMonth)
    --------------------
    _# Reporting Month = 
    VAR
    maxReportingMonth = CALCULATE(MAX('Table'[Reporting Month]),ALLSELECTED('Table'[Reporting Month]))
    RETURN
    maxReportingMonth
    --------------------
    _Status = 
    VAR
    maxReportingMonth = CALCULATE(MAX('Table'[Reporting Month]),ALLSELECTED('Table'[Reporting Month]))
    RETURN
    CALCULATE(SELECTEDVALUE('Table'[Status]),'Table'[Reporting Month]= maxReportingMonth)

     

    • adhossain's avatar
      adhossain
      Frequent Visitor

      Hi Payeras_BI 

      At first thank you for your suggestions. But this returns entries of duplicate items. I only want to return the values from recent dates only fro the same Unique IDs. Kindly see below HIGHLIGHTED rows which I only want to retrieve.

       

      • For first three months

       

      • For first four months

       

      • For first 5 months

      • For first six months

       

      Thanks in advanced.

       

      Attaching the sample files if you want to exercise.

      Excel data sheet 

      Power Bi report

      • Payeras_BI's avatar
        Payeras_BI
        Solution Sage

        Hi adhossain ,

        I have checked your file and seen that you have written the measures but you failed to drag on of them to the table visualization.

        Please substitute the Status field by the status measure and come back to me with the result.