Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM. Register now.

Reply
cgyan9
Regular Visitor

Creating a conditional table from other tables

Hi All, I am hoping to create a new table C with certain values from Table A or Table B -
Table C has the first column as Month - Year, the 2nd column should check the calculated measure of say hrs on a monthly basis from Table A and take that value else if there is no value for that particular month in Table A, it should take the calculated sum value in Table B for that particular month (Table B has values for all the months).
Thanks in advance for all the help!  

1 ACCEPTED SOLUTION

 

let
    Source = Table.SelectRows(#"Table 1", each ([Location] <> " ")) & #"Table 2",
    #"Removed Duplicates" = Table.Distinct(Source, {"Month Yr", "Location"})
in
    #"Removed Duplicates"

lbendlin_0-1722974097144.png

 

 

View solution in original post

4 REPLIES 4
p45cal
Super User
Super User

You have your table C set up with a year/month column,

1. you merge it with table A in one column (let's give this column the header AA),

2. you merge it with table B in another column (we'll head it with BB),

3. you add another column (say headed Result) which examines column AA and if there's an empty table (no data in Table A for that year/Month) copies the BB column to itself, else copies the AA column to itself.

4. Then remove columns AA and BB and

5. process the Result column.

 

Why don't you set up an Excel Workbook with a facsimile of these tables and provide a link to it so that someone can put some M-code together to demonstrate?

 

Here's an example file: https://app.box.com/s/xahhaoc05meyghp81xabh1x0r52sr918

Here's the code generated in it:

let
    Source = DatesTable,
    #"Merged Queries" = Table.NestedJoin(Source, {"Year/Month"}, TableA, {"YearMonth"}, "AA", JoinKind.LeftOuter),
    #"Merged Queries1" = Table.NestedJoin(#"Merged Queries", {"Year/Month"}, TableB, {"YearMonth"}, "BB", JoinKind.LeftOuter),
    #"Added Custom" = Table.AddColumn(#"Merged Queries1", "Result", each if Table.IsEmpty([AA]) then [BB] else [AA]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"AA", "BB"}),
    #"Expanded Result" = Table.ExpandTableColumn(#"Removed Columns", "Result", {"Data1", "Data2"}, {"Data1", "Data2"})
in
    #"Expanded Result"
lbendlin
Super User
Super User

You posted this in the Power Query section.  Power Query does not support dynamic source creation. 

 

In DAX you can create calulated tables, or temporary table variables for use in measures.

 

Please provide sample data that covers your issue or question completely, in a usable format (not as a screenshot).

Do not include sensitive information or anything not related to the issue or question.

If you are unsure how to upload data please refer to https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-...

Please show the expected outcome based on the sample data you provided.

Want faster answers? https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447...

Sharing an outline of the tables that I have -
Table 1 

 

Month YrLocationFunctionHrs
Jan-24AAA1
Jan-24BBB2
Mar-24CCC3
Apr-24   

 

Table 2 -

 

Month YrLocationFunctionHrs
Mar-24CCC1
Apr-24BBB2
May-24CCC2

 

Output Result Table - It should 1st check for values in Table 1, if blank in Table 1 then take values from Table 2 

Month YrLocationFunctionHrs
Jan-24AAA1
Jan-24BBB2
Mar-24CCC3
Apr-24BBB2
May-24CCC2

 

 

let
    Source = Table.SelectRows(#"Table 1", each ([Location] <> " ")) & #"Table 2",
    #"Removed Duplicates" = Table.Distinct(Source, {"Month Yr", "Location"})
in
    #"Removed Duplicates"

lbendlin_0-1722974097144.png

 

 

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Kudoed Authors