Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
9 years ago
Solved

Create new table by filtering column value

Hi,

 

Please let me know how to create new table from one Big table. I would like to create 3-4 small tables from one big based on one the column value.

 

Thanks

Harish

  • This is the "M" code for the main table:

     

    let
        Source = Csv.Document(File.Contents("C:\temp\powerbi\departments.csv"),[Delimiter=",", Columns=3, Encoding=1252, QuoteStyle=QuoteStyle.None]),
        #"Promoted Headers" = Table.PromoteHeaders(Source),
        #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Department", Int64.Type}, {"Name", type text}, {"DeptType", type text}})
    in
        #"Changed Type"

    For DeptType A

     

    let
        Source = Csv.Document(File.Contents("C:\temp\powerbi\departments.csv"),[Delimiter=",", Columns=3, Encoding=1252, QuoteStyle=QuoteStyle.None]),
        #"Promoted Headers" = Table.PromoteHeaders(Source),
        #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Department", Int64.Type}, {"Name", type text}, {"DeptType", type text}}),
        #"Filtered Rows" = Table.SelectRows(#"Changed Type", each ([DeptType] = "A"))
    in
        #"Filtered Rows"

    For DeptType B

     

    let
        Source = Csv.Document(File.Contents("C:\temp\powerbi\departments.csv"),[Delimiter=",", Columns=3, Encoding=1252, QuoteStyle=QuoteStyle.None]),
        #"Promoted Headers" = Table.PromoteHeaders(Source),
        #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Department", Int64.Type}, {"Name", type text}, {"DeptType", type text}}),
        #"Filtered Rows" = Table.SelectRows(#"Changed Type", each ([DeptType] = "B"))
    in
        #"Filtered Rows"

    Red text indicates the only real difference in the queries. You can filter a column by clicking on the down arrow in the column in the Query Editor and just selecting the value(s) that you want.

     

    Again, I'm not entirely clear on the use case. If you are new to Power BI, you will find that many of the things that you might typically create a star schema for in traditional multi-dimensional cubes are not entirely necessary in Power BI due to how slicers work, etc. So, if you want a particular measure or calculation to just be on DeptType A or DeptType B, you can put that column in a visual along with your measure and the context of the visual will give you the right answer. Just throwing that out there because it is something I had to slowly realize over time coming from a more traditional BI background.

  • Anonymous's avatar
    Anonymous
    9 years ago

    Hi Anonymous,

     

    Smoupre’s solution seems well, I’d like to share other way to solve your issue based on dax:

     

    SubTable A = CALCULATETABLE(“Main Table”,FILTER(“Main Table”, “Main Table”[DeptType]="A"))

    SubTable B = CALCULATETABLE(“Main Table”,FILTER(“Main Table”, “Main Table”[DeptType]="B"))

     

    Regards,

    Xiaoxin Sheng

     

12 Replies

  • Greg_Deckler's avatar
    Greg_Deckler
    Community Champion

    You could just copy your query in the Query Editor, filter your column, import it into a new table. Rinse and repeat for as many tables as you desire. Not entirely sure of the use case, but this would be one way of doing it.

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thanks for replying. Could you please share query for this, I am new to BI. Below in my scenario :

       

       

      • Greg_Deckler's avatar
        Greg_Deckler
        Community Champion

        This is the "M" code for the main table:

         

        let
            Source = Csv.Document(File.Contents("C:\temp\powerbi\departments.csv"),[Delimiter=",", Columns=3, Encoding=1252, QuoteStyle=QuoteStyle.None]),
            #"Promoted Headers" = Table.PromoteHeaders(Source),
            #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Department", Int64.Type}, {"Name", type text}, {"DeptType", type text}})
        in
            #"Changed Type"

        For DeptType A

         

        let
            Source = Csv.Document(File.Contents("C:\temp\powerbi\departments.csv"),[Delimiter=",", Columns=3, Encoding=1252, QuoteStyle=QuoteStyle.None]),
            #"Promoted Headers" = Table.PromoteHeaders(Source),
            #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Department", Int64.Type}, {"Name", type text}, {"DeptType", type text}}),
            #"Filtered Rows" = Table.SelectRows(#"Changed Type", each ([DeptType] = "A"))
        in
            #"Filtered Rows"

        For DeptType B

         

        let
            Source = Csv.Document(File.Contents("C:\temp\powerbi\departments.csv"),[Delimiter=",", Columns=3, Encoding=1252, QuoteStyle=QuoteStyle.None]),
            #"Promoted Headers" = Table.PromoteHeaders(Source),
            #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Department", Int64.Type}, {"Name", type text}, {"DeptType", type text}}),
            #"Filtered Rows" = Table.SelectRows(#"Changed Type", each ([DeptType] = "B"))
        in
            #"Filtered Rows"

        Red text indicates the only real difference in the queries. You can filter a column by clicking on the down arrow in the column in the Query Editor and just selecting the value(s) that you want.

         

        Again, I'm not entirely clear on the use case. If you are new to Power BI, you will find that many of the things that you might typically create a star schema for in traditional multi-dimensional cubes are not entirely necessary in Power BI due to how slicers work, etc. So, if you want a particular measure or calculation to just be on DeptType A or DeptType B, you can put that column in a visual along with your measure and the context of the visual will give you the right answer. Just throwing that out there because it is something I had to slowly realize over time coming from a more traditional BI background.

  • Hi,

     

    Is there a way I can make a new/seperate table for every distinct value, no matter if my input changes?

    e.g. at the moment I havel 12 different values = 12 tables, 12 times modifying the query script as it were from 

        #"Filtered Rows" = Table.SelectRows(#"Changed Type", each ([DeptType] = "A")) to [DeptType] = "L"

    I would like to be able to create these 12 tables in the same script and, if the numer of disctinct values changes (e.g. to 13), create the extra table automatically also