Forum Discussion

rayinOz's avatar
rayinOz
Helper III
9 years ago
Solved

Finding multiple entries / Creating new table

Hello,   So I have a table that has the following columns:   | Username | Course Name | Course Status |   The table contains data related to course enrolments/completions.   There is Course A...
  • MarcelBeug's avatar
    9 years ago

    Power Query solution:

    You can use remove and keep duplicates:

    If you can have multiple records per Username/Course Name combination: first select Username and Course Name and remove duplicates (Home - Remove Rows - Remove Duplicates)

    Select Username and select Home - Keep Rows - Keep duplciates

    Select Username and select Home - Remove Rows - Remove Duplciates

    Remove the other columns.

     

    Resulting code:

    let
        Source = CourseEnrolments,
        #"Removed Duplicates" = Table.Distinct(Source, {"Username", "Course Name"}),
        #"Kept Duplicates" = let columnNames = {"Username"}, addCount = Table.Group(#"Removed Duplicates", columnNames, {{"Count", Table.RowCount, type number}}), selectDuplicates = Table.SelectRows(addCount, each [Count] > 1), removeCount = Table.RemoveColumns(selectDuplicates, "Count") in Table.Join(#"Removed Duplicates", columnNames, removeCount, columnNames, JoinKind.Inner),
        #"Removed Duplicates1" = Table.Distinct(#"Kept Duplicates", {"Username"}),
        #"Removed Columns" = Table.RemoveColumns(#"Removed Duplicates1",{"Course Name", "Course Status"})
    in
        #"Removed Columns"

     

  • MarcelBeug's avatar
    MarcelBeug
    9 years ago

    Yes: before removing duplicates, sort descending on date, wrap the code in Table.Buffer(....) and then remove duplicates.

     

    In case you want to preserve the original sort order: add an index column first.
    After removing duplciates, sort on that index column and remove the index column