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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
CloudMonkey
Post Prodigy
Post Prodigy

Power Query M code for referring to another table

Hello,

 

Please can you tell me how to do a 'countifs' between two tables using Power Query?

 

In table 1 I have:

Project Start Date

Proejct End Date

 

In table 2 I have a calendar of flagged working days

Date

IsDateAWorkingDay

 

Using Power Query I'd like to add a column to table 1 to give the number of working days that a project lasted. It would equal the number of dates in table 2 that are greater than or equal to the [Project Start Date] and less than or equal to the [Project End Date] and where [IsDateAWorkingDay] is "True".

 

Please can you tell me if this is possible?

 

Thanks,

 

CM

1 ACCEPTED SOLUTION
v-yulgu-msft
Microsoft Employee
Microsoft Employee

Hi @CloudMonkey,

 

Please try below steps in Query Editor:

 

1) Suppose there is a Project column in table1. If not, please add an index column. Select the start date and end date columns in table1 and change data type to whole number.

1.PNG2.PNG

 

2) add a custom column.

={[Project Start Date]..[Project End Date]}

3.PNG

 

3) expand to new rows and rename and convert back to dates

4.PNG5.PNG

4) Merge table1 and table2 (Home -> Merge Queries). Expand necessary columns.

6.PNG7.PNG

 

5) Filter [IsDateWorkingDay] to make tabe only show working dates.

8.PNG

 

6) Group table.

9.PNG

 

7) Result.

10.PNG

 

The entire M code.

let
    Source = Excel.Workbook(File.Contents("C:\Users\xxxxx\Desktop\xxxxx.xlsx"), null, true),
    table_1_Sheet = Source{[Item="table_1",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(table_1_Sheet, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"ProjectNo", Int64.Type}, {"Project Start Date", type date}, {"Project End Date", type date}}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Changed Type",{{"Project Start Date", Int64.Type}, {"Project End Date", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type1", "Date", each {[Project Start Date]..[Project End Date]}),
    #"Expanded Date" = Table.ExpandListColumn(#"Added Custom", "Date"),
    #"Changed Type2" = Table.TransformColumnTypes(#"Expanded Date",{{"Project Start Date", type date}, {"Project End Date", type date}, {"Date", type date}}),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type2",{"Date"},table_2,{"Date"},"table_2",JoinKind.Inner),
    #"Expanded table_2" = Table.ExpandTableColumn(#"Merged Queries", "table_2", {"IsDateWorkingDay"}, {"table_2.IsDateWorkingDay"}),
    #"Filtered Rows" = Table.SelectRows(#"Expanded table_2", each ([table_2.IsDateWorkingDay] = true)),
    #"Grouped Rows" = Table.Group(#"Filtered Rows", {"ProjectNo", "Project Start Date", "Project End Date"}, {{"CountWoringDay", each Table.RowCount(_), type number}})
in
    #"Grouped Rows"

Best regards,
Yuliana Gu

Community Support Team _ Yuliana Gu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

3 REPLIES 3
v-yulgu-msft
Microsoft Employee
Microsoft Employee

Hi @CloudMonkey,

 

Please try below steps in Query Editor:

 

1) Suppose there is a Project column in table1. If not, please add an index column. Select the start date and end date columns in table1 and change data type to whole number.

1.PNG2.PNG

 

2) add a custom column.

={[Project Start Date]..[Project End Date]}

3.PNG

 

3) expand to new rows and rename and convert back to dates

4.PNG5.PNG

4) Merge table1 and table2 (Home -> Merge Queries). Expand necessary columns.

6.PNG7.PNG

 

5) Filter [IsDateWorkingDay] to make tabe only show working dates.

8.PNG

 

6) Group table.

9.PNG

 

7) Result.

10.PNG

 

The entire M code.

let
    Source = Excel.Workbook(File.Contents("C:\Users\xxxxx\Desktop\xxxxx.xlsx"), null, true),
    table_1_Sheet = Source{[Item="table_1",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(table_1_Sheet, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"ProjectNo", Int64.Type}, {"Project Start Date", type date}, {"Project End Date", type date}}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Changed Type",{{"Project Start Date", Int64.Type}, {"Project End Date", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type1", "Date", each {[Project Start Date]..[Project End Date]}),
    #"Expanded Date" = Table.ExpandListColumn(#"Added Custom", "Date"),
    #"Changed Type2" = Table.TransformColumnTypes(#"Expanded Date",{{"Project Start Date", type date}, {"Project End Date", type date}, {"Date", type date}}),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type2",{"Date"},table_2,{"Date"},"table_2",JoinKind.Inner),
    #"Expanded table_2" = Table.ExpandTableColumn(#"Merged Queries", "table_2", {"IsDateWorkingDay"}, {"table_2.IsDateWorkingDay"}),
    #"Filtered Rows" = Table.SelectRows(#"Expanded table_2", each ([table_2.IsDateWorkingDay] = true)),
    #"Grouped Rows" = Table.Group(#"Filtered Rows", {"ProjectNo", "Project Start Date", "Project End Date"}, {{"CountWoringDay", each Table.RowCount(_), type number}})
in
    #"Grouped Rows"

Best regards,
Yuliana Gu

Community Support Team _ Yuliana Gu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Anonymous
Not applicable

@CloudMonkey

Yes, this is feasible.  The M solution is a little lengthy, but the Calculated Column method in DAX is a little faster (but it won't compress as well).

Table1[# of Working Days Column] =
CALCULATE (
    COUNTROWS ( Table2 ),
    FILTER (
        Table2,
        Table2[Date] >= Table1[Project Start Date]
            && Table2[Date] <= Table1[Project End Date]
    ),
    Table2[IsDateAWorkingDay] = "True"
)

I don't have the time to post the M solution right now, but let me know if you're interested in it.

 

Hi Chris - thanks but I would need to use Power Query / M code, so that I have the flexibility to be able to append tables later on.

 

Thanks,

 

CM

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors