Forum Discussion
PowerQuery merge queries for latest data in history table
I have 2 queries in PowerQuery, 1 is from a BookingEvent table where each event has a [StartDate] field and 15 associated revenue fields [ActualRevenue1] through [ActualRevenue15]; I have a calculated column that sums these 15 columns [TotalActualRevenue]. The [Id] field is the primary key for this table.
Related to this, I have a query pulling data from a BookingEventHistory table that tracks any event changes, and contains [ParentID] (the FK to the BookingEvent table), [dtCreated] (the date of the change), [Field] (the name of the event field that has been changed -- I only care about those 15 revenue fields for now), and [NewValue] (the change made to the event field).
I'm trying to create either a merge query, new table, or a DAX measure (or anything of the sort) that would allow me to get the most recent values for [ActualRevenue1] through [ActualRevenue15] based on the most recent change less than or equal to the date for which the user has set the date filter. I was thinking that the easiest way to do this would be to merge the 2 queries as a new query, with each change date representing a date record in the new query, and the most recent value for [ActualRevenue1] through [ActualRevenue15] being the values in this merged query, something like:
Transaction / Change Date ActualRevenue1 ActualRevenue2 ... ActualRevenue15 TotalActualRevenue
----------------------------- ----------------- ----------------- -- -------------------- ---------------------
first entry in BookingEvent $n $n $n $n
first change in History tbl $n+n $n+n $n+n $n+n
...
last change in History tbl $n+n(n) $n+n(n) $n+n(n) $n+n(n)
where if there is a change to only 1 or a few of the revenue fields for a given date (which is most often), the remaining fields are populated with the most recent value available. Or, I guess they could be null too, as long as I can query the most recent values for each field and sum the revenues in the [TotalActualRevenue] column.
Then, I would just lookup this new table to return the most recent revenue figures based on the user's filter. The challenge is I'm not sure how to do this in PowerQuery, or if there's a better way to accomplish this.
Thanks for your thoughts!