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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Anonymous
Not applicable

How to merge 2 tables using interval of dates

Hello!

In my previous post I asked how to prepare dataset for resource load.

For example, for every row of db_task table

Nikolai_0-1642676193226.png

I need to recieve this in another table:

Nikolai_2-1642676373917.png

The solution was to add a table on DAX:

 

clx_ResourcePlans = 
    SUMMARIZE(
        filter(
            CROSSJOIN(db_assignment, Local_DateTime),
					Local_DateTime[Date]>=db_assignment[planned_start]
					&& Local_DateTime[Date]<=db_assignment[planned_finish]
					&& NOT(Local_DateTime[IsHolidayInRussia])
        ),
        db_assignment[task_id], db_assignment[user_id], Local_DateTime[Date], "_plan", round(sum(db_assignment[DailyHours]), 2), "_fact", 0
    )

 

Then, I made another table for facts:

 

clx_ResourceFacts = 
    SUMMARIZE(
        FILTER(
            CROSSJOIN(db_task, db_time_entry),
			db_task[id]==db_time_entry[task_id]
            && db_time_entry[is_billable]="t")
        ,
        db_time_entry[task_id], db_time_entry[user_id], db_time_entry[for_date], "_plan", 0, "_fact", round(sum(db_time_entry[hours]), 2)
    )

 

In order to compare plans an facts, I append two tables:

 

clx_ResourceLoad = UNION(clx_ResourcePlans, clx_ResourceFacts)

 

Now the problem is that UNION expects, that I need two tables with the same structure. The same structure means the same number of columns. The matching is based on the position of the column in the table.

As you can see, I even add "_plan" column to clx_ResourceFacts table and "_fact" - to clx_ResourcePlans table.

But it did not help. My tables have different sequence of columns. An I get something like this

Nikolai_3-1642677469415.png

How can I solve this? 

I read, that it is strongly recommended to have a look at Append and Merge transformations in Power Query instead of combining tables in DAX.

But I don't know how to merge tables in DAX using advanced condition, like

 

Local_DateTime[Date]>=db_assignment[planned_start]
&& Local_DateTime[Date]<=db_assignment[planned_finish]

 

 

1 ACCEPTED SOLUTION
Greg_Deckler
Super User
Super User

@Anonymous If I am following this, use SELECTCOLUMNS to get your two tables to have the same order and names of columns prior to UNION.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

1 REPLY 1
Greg_Deckler
Super User
Super User

@Anonymous If I am following this, use SELECTCOLUMNS to get your two tables to have the same order and names of columns prior to UNION.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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