[Power Query] - Combine 2 queries/tables with valid From and to date fields

Bullstrik1

Board Regular
Joined
Jul 31, 2014
Messages
66
Hi everyone!

I'v been googling this issue for quite some time now, but I wasn't able to find anything related for porwer query so far, and thats why i'm counting with some of your experts help. :)

To the point:

I have 2 tables i would like to join, based on some date fields (I'm sorry about the unexistant formatting but i don't know how to post Excel tables in this forum, but i'll be glad to post an example excel workbook if you tell me how to :P )

Fact table:

OcurrenceObjectEntry DateOut Date
A108-02-2017
B207-02-201707-02-2017
C124-12-201624-12-2016
D315-01-201715-01-2017
E316-01-2017

<tbody>
</tbody>

<tbody>
</tbody>
Dimensions table:

PersonObjectDateFromDateTo
1105-02-201715-02-2017
1301-01-201731-01-2017
2101-12-201604-02-2017
2205-02-201731-12-2100

<tbody>
</tbody>

From this 2 tables, i would like to create a join that would return the folowing table:

OcurrenceEntry DateOut DatePersonObjectDateFromDateTo
A08-02-20171105-02-201715-02-2017
D15-01-201715-01-20171301-01-201731-01-2017
E16-01-20171301-01-201731-01-2017
C24-12-201624-12-20162101-12-201604-02-2017
B07-02-201707-02-20172205-02-201731-12-2100

<tbody>
</tbody>

Note:
As you can see, sometimes Entry Date is null and there is no way one can retreive that information.

Anyone have an idea how one could do this using power query?

I saw this post with somehting similar to what i want to do but for SQL:
https://blog.oraylis.de/2014/11/com...d-fromto-date-ranges-into-a-single-dimension/
Hopefully it will provide some better insight on what i would like to do.

Thank you, in advance for your help and fr reading this post! :)
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
As a first reply: maybe the answer in this post might be helpful. It looks somewhat similar.

Otherwise, for your specific case, can you provide some more information on the logic of combining the 2 tables (which dates should be before/after which dates, also in case of missing dates (which dates can be missing?), is any overlap possible, i.e. mulitple hits, etcetera).

Remark: I probably won't be available for any further feedback during the next 8 hours or so.
 
Upvote 0
I created the tables Facts and Dimensions.
And the function GetPerson:

Code:
(Object as number, Out as date, optional Entry as date) => 
        Table.SelectRows(Dimensions, 
                         each [Object] = Object and [DateFrom] <= Out and 
                              (if Entry = null then [DateTo] >= Out 
                                               else [DateTo] >= Entry))

And the following query:
Code:
let
    Source = Facts,
    #"Invoked Custom Function" = Table.AddColumn(Source, "GetPerson", each GetPerson([Object], [Out Date], [Entry Date])),
    #"Expanded GetPerson" = Table.ExpandTableColumn(#"Invoked Custom Function", "GetPerson", {"Person", "DateFrom", "DateTo"}, {"Person", "DateFrom", "DateTo"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Expanded GetPerson",{"Occurrence", "Entry Date", "Out Date", "Person", "Object", "DateFrom", "DateTo"})
in
    #"Reordered Columns"
Apart from the sort order (which I don't understand) this produces the output according to your example.
 
Last edited:
Upvote 0
Hi everyone!

MarcelBeug, thank you so very much for your reply and help!
I haven't tested your queries yet (lack of time i'm so sorry), but i will test it soon enough.

The logic/rules of combining the 2 tables is the folowing:

1. One person is owner of a certain object in a given time span;
2. One person can only have one single object in a certain time spam;
3. While the person is owning the object in a given time span all ocurrences, in the same time span, must be linked to that same person;
4. All ocurrences have an Out date, but it can happen to not have and Entry date
5. One ocurrence max time span in 24h, or one single day - its not possible one ocurrence to have Entry date on day x and Out date on day x+n, being n any natural number superior or equal to 1

Thank you! :)
 
Upvote 0
Hi everyone!

MarcelBeug, thank you so very much for your reply and help!
I haven't tested your queries yet (lack of time i'm so sorry), but i will test it soon enough.

The logic/rules of combining the 2 tables is the folowing:

1. One person is owner of a certain object in a given time span;
2. One person can only have one single object in a certain time spam;
3. While the person is owning the object in a given time span all ocurrences, in the same time span, must be linked to that same person;
4. All ocurrences have an Out date, but it can happen to not have and Entry date
5. One ocurrence max time span in 24h, or one single day - its not possible one ocurrence to have Entry date on day x and Out date on day x+n, being n any natural number superior or equal to 1

Thank you! :)



Hi everyone!

I finally had the time to test this out.
It works like a charm!

I just need to test this with my real data now, but i'm sure it will be allright, and if not i'll post back here.

And thanks again Marcel!

Cheers :)
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,698
Members
448,979
Latest member
DET4492

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top