Hi
I've recently started dabling with powerbi. I have a scenario where I want to left join two tables with an aditional condition checking if the date values from table A is between a Start and End Date in Table B.
If the data is not present in Table B then Table A's rows still needs to be returned. In SQL this is a pretty straight forward query, but for the life of me i'm strugling to figure this out in Powerbi.
What would the syntax be in either DAX or M Query to achieve the same result set as posted below ?
Bellow I have listed the 2 tables , sample query written in SQL and the expected results. I have also attached the scripts to generate the sample set.
Thanks in advance 🙂
Table : ta
Column | Data Type |
RD | datetime |
Schedule | int |
Dataset | int |
Table : tb
Column | Data Type |
SD | datetime |
ED | datetime |
Dataset | int |
RowsCopied | int |
SQL : Query :
select
a.RD as a_eventDate ,
a.Schedule as a_Schedule,
a.Dataset as a_Dataset,
b.SD as b_SD ,
b.ED as b_ED,
b.Dataset as b_Dataset,
b.RowsCopied as b_RowsCopied
from ta as a
left join tb as b on a.Dataset = b.Dataset
and a.RD >= b.SD and a.RD <= b.ED
SQL Results :
a_eventDate | a_Schedule | a_Dataset | b_SD | b_ED | b_Dataset | b_RowsCopied |
2020-01-01 08:00:00 | 1 | 1 | 2020-01-01 07:45:00 | 2020-01-01 08:45:00 | 1 | 300 |
2020-01-01 08:00:00 | 1 | 2 | 2020-01-01 07:47:00 | 2020-01-01 08:25:00 | 2 | 100 |
2020-01-01 08:00:00 | 1 | 3 | NULL | NULL | NULL | NULL |
2020-01-01 08:00:00 | 1 | 4 | 2020-01-01 07:45:00 | 2020-01-01 08:35:00 | 4 | 10 |
2020-01-01 08:00:00 | 1 | 5 | NULL | NULL | NULL | NULL |
2020-01-01 14:00:00 | 2 | 6 | NULL | NULL | NULL | NULL |
2020-01-01 14:00:00 | 2 | 2 | 2020-01-01 14:00:00 | 2020-01-01 14:15:00 | 2 | 25 |
2020-01-02 08:00:00 | 1 | 1 | 2020-01-02 07:45:00 | 2020-01-02 08:45:00 | 1 | 30 |
2020-01-02 08:00:00 | 1 | 2 | 2020-01-02 07:45:00 | 2020-01-02 08:05:00 | 2 | 40 |
2020-01-02 08:00:00 | 1 | 3 | 2020-01-02 07:45:00 | 2020-01-02 08:15:00 | 3 | 20 |
2020-01-02 08:00:00 | 1 | 4 | NULL | NULL | NULL | NULL |
2020-01-02 08:00:00 | 1 | 5 | NULL | NULL | NULL | NULL |
2020-01-02 14:00:00 | 2 | 6 | 2020-01-02 14:00:00 | 2020-01-02 14:15:00 | 6 | 20 |
2020-01-02 14:00:00 | 2 | 2 | NULL | NULL | NULL | NULL |
Creating the sample data set scripts:
create table ta (RD datetime , Schedule int , Dataset int)
create table tb (SD datetime , ED datetime, Dataset int , RowsCopied int)
insert into ta values('2020-01-01 8:00' ,1,1 )
insert into ta values('2020-01-01 8:00' ,1,2 )
insert into ta values('2020-01-01 8:00' ,1,3 )
insert into ta values('2020-01-01 8:00' ,1,4 )
insert into ta values('2020-01-01 8:00' ,1,5 )
insert into ta values('2020-01-01 14:00' ,2 ,6 )
insert into ta values('2020-01-01 14:00' ,2 ,2 )
insert into ta values('2020-01-02 8:00' ,1,1 )
insert into ta values('2020-01-02 8:00' ,1,2 )
insert into ta values('2020-01-02 8:00' ,1,3 )
insert into ta values('2020-01-02 8:00' ,1,4 )
insert into ta values('2020-01-02 8:00' ,1,5 )
insert into ta values('2020-01-02 14:00' ,2,6 )
insert into ta values('2020-01-02 14:00' ,2,2 )
insert into tb values('2020-01-01 7:45','2020-01-01 8:45' ,1,300)
insert into tb values('2020-01-01 7:47','2020-01-01 8:25' ,2,100)
insert into tb values('2020-01-01 7:45','2020-01-01 8:35' ,4,10)
insert into tb values('2020-01-02 7:45','2020-01-02 8:45' ,1,30)
insert into tb values('2020-01-02 7:45','2020-01-02 8:05' ,2,40)
insert into tb values('2020-01-02 7:45','2020-01-02 8:15' ,3,20)
insert into tb values('2020-01-02 14:00','2020-01-02 14:15' ,6,20)
insert into tb values('2020-01-01 14:00','2020-01-01 14:15' ,2,25)