cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Frequent Visitor

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

2 ACCEPTED SOLUTIONS

Accepted Solutions
Highlighted
Super User IX
Super User IX

Re: Create new table by filtering column value

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.


---------------------------------------

@ me in replies or I'll lose your thread!!!

I have a NEW book! 
DAX Cookbook from Packt
Over 120 DAX Recipes!




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

Highlighted
Community Support
Community Support

Re: Create new table by filtering column value

Hi @harishkanyal,

 

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

 

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

View solution in original post

10 REPLIES 10
Highlighted
Super User IX
Super User IX

Re: Create new table by filtering column value

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.


---------------------------------------

@ me in replies or I'll lose your thread!!!

I have a NEW book! 
DAX Cookbook from Packt
Over 120 DAX Recipes!




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Highlighted
Frequent Visitor

Re: Create new table by filtering column value

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

 

 Capture.PNG

Highlighted
Super User IX
Super User IX

Re: Create new table by filtering column value

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.


---------------------------------------

@ me in replies or I'll lose your thread!!!

I have a NEW book! 
DAX Cookbook from Packt
Over 120 DAX Recipes!




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

Highlighted
Frequent Visitor

Re: Create new table by filtering column value

Thanks for details email. My main table data is alreday in table which I imorted from other source . To make reports I need to createe new small table. Seems I can use below part of code to filter rows from main table, Let me try it during weekend :

 

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

Highlighted
Community Support
Community Support

Re: Create new table by filtering column value

Hi @harishkanyal,

 

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

 

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

View solution in original post

Highlighted
Regular Visitor

Re: Create new table by filtering column value

hi there,

 

kinda related... i also am looking for a way to split a main table into several small flitered duplicates, however i want it to happen dynamically as i can't be sure what the actual filter values will be (or there may be too many for me to bother writing code for each and every value) 😄

 

Is there a way to code M to do this for each unique value in a coloumn without specifying those values ?

 

kind regards

Michael

Highlighted
Regular Visitor

Re: Create new table by filtering column value

Hi Greg,

 

How would you make the filtering value dynamic?

 

I have a main table with orders from many different customers and they all need their own report and it changes each month who is in the report. I don't want to write filter code for each customer as there can be as many as 300.

 

So i'm looking for a way to have powerquery or powerpivot do somekind of new table for each unique customer id with all of that customers orders from the main table.

Highlighted
Anonymous
Not applicable

Re: Create new table by filtering column value

If I have a dynamic measure instead of "A","B"

Suppose Measure1="C", Measure2="A" (dynamic based on some formulas, not fixed)

How will I calculate a table based on a master table, filtering only "C" values and storing in a table1, A values in Table2

MASTERTABLETable1 Table2
     
MilestoneMilestoneMilestone
A C A
A C A
A   A
B    
B    
B    
C    
C    
Highlighted
Post Partisan
Post Partisan

Re: Create new table by filtering column value

This is such a great answer! Thanks, man, I'm using it now.

Helpful resources

Announcements
Upcoming Events

Upcoming Events

Wondering what events you could join or have an event to promote yourself? Check out our Upcoming Events.

Upcoming Events

Community Summit North America – Join Online!

Join this community-driven Power Platform digital event for unbiased support and problem-solving.

Experience what’s next for Power BI

Join us for an in-depth look at the new Power BI features and capabilities at the free Microsoft Business Applications Launch Event.

Community Blog

Community Blog

Visit our Community Blog for articles, guides, and information created by fellow community members.

Top Solution Authors