Forum Discussion

Richard_D's avatar
Richard_D
New Member
4 years ago
Solved

List unique items in table

Hi,

 

I am trying to replicate something in Power BI that was easy to create in Excel. I have a list of items and want to show for each year what unique items exist in that list. Example date is:

YearValue
2021A
2021A
2021B
2021C
2021C
2021C
2022A
2022D
2022D
2023C
2023C
2023E
2023F
2023G
2023G
2023H
2024H
2024H
2024I
2024J

 

Using formula =UNIQUE(FILTER($G$7:$G$26;$F$7:$F$26=J$6;TRUE)) in Excel I get the desired result:

2021202220232024
AACH
BDEI
C FJ
  G 
  H 

 

How can I create something similar in Power BI. The total list contains about 30.000 items with 0 - 50 unique items per year.

  • Anonymous's avatar
    Anonymous
    4 years ago

    Hi Richard_D ,

     

    1. Change the Year and Values column type to Text.

    2.Right-click the blank field -->choose New Query-->to create a Blank Query

     

    3.Open the Advanced Editor dialog, enter the following formula:

    (Source as table
    ,ColToPivot as text
    ,ColForValues as text)=> 
    let
         PivotColNames = List.Buffer(List.Distinct(Table.Column(Source,ColToPivot))) 
        ,#"Pivoted Column" = Table.Pivot(Source, PivotColNames, ColToPivot, ColForValues, each _)
     
        ,TableFromRecordOfLists = (rec as record, fieldnames as list) =>
        let
            PartialRecord = Record.SelectFields(rec,fieldnames)
           ,RecordToList = Record.ToList(PartialRecord)
           ,Table = Table.FromColumns(RecordToList,fieldnames)
        in
            Table
     
        ,#"Added Custom" = Table.AddColumn(#"Pivoted Column", "Values", each TableFromRecordOfLists(_,PivotColNames))
        ,#"Removed Other Columns" = Table.RemoveColumns(#"Added Custom",PivotColNames)
        ,#"Expanded Values" = Table.ExpandTableColumn(#"Removed Other Columns", "Values", PivotColNames)
    in
        #"Expanded Values"

    4.Enter parematers:

     

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

3 Replies

  • You can concatenate the values like this:

    UniqueValues = CONCATENATEX ( VALUES ( Table1[Value] ), Table1[Value], UNICHAR(10) )

    It may look better horizontally (use ", " or something instead of UNICHAR(10) as the separator).

  • AilleryO's avatar
    AilleryO
    Memorable Member

    Hi,

     

    You can try somthing like this in DAX, many solutions in M as well :

    CONCATENATEX(DISTINCT('Tb_Valeur_Uniques'),'Tb_Valeur_Uniques'[Value],UNICHAR(10))
     
    CONCATENATEX put the value together,
    DISTINCT to avoid duplicates
    UNICHAR(10) to make line feed between values.
     
     
    Hope it helps
  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Richard_D ,

     

    1. Change the Year and Values column type to Text.

    2.Right-click the blank field -->choose New Query-->to create a Blank Query

     

    3.Open the Advanced Editor dialog, enter the following formula:

    (Source as table
    ,ColToPivot as text
    ,ColForValues as text)=> 
    let
         PivotColNames = List.Buffer(List.Distinct(Table.Column(Source,ColToPivot))) 
        ,#"Pivoted Column" = Table.Pivot(Source, PivotColNames, ColToPivot, ColForValues, each _)
     
        ,TableFromRecordOfLists = (rec as record, fieldnames as list) =>
        let
            PartialRecord = Record.SelectFields(rec,fieldnames)
           ,RecordToList = Record.ToList(PartialRecord)
           ,Table = Table.FromColumns(RecordToList,fieldnames)
        in
            Table
     
        ,#"Added Custom" = Table.AddColumn(#"Pivoted Column", "Values", each TableFromRecordOfLists(_,PivotColNames))
        ,#"Removed Other Columns" = Table.RemoveColumns(#"Added Custom",PivotColNames)
        ,#"Expanded Values" = Table.ExpandTableColumn(#"Removed Other Columns", "Values", PivotColNames)
    in
        #"Expanded Values"

    4.Enter parematers:

     

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