Creating a join expression with multiple criteria
Hi,
I am a little stuck trying to create a join expression. In the table below, I want to join the Cost using the active From date to a table of data with multiple dates. For example, I have data from 1/31/24 thru today. For Campaign 1, I want to use the cost of .5 up to 1/31/2025 and then moving forward after that, use .6 as the cost.
I currently have the expression Raw Data
.Date
>= Table Below
.Active From
which will only work on one of the 2 dates. I have thought about using Upper and Lower, but then I think about, "What if I have a 3rd option for Campaign 1.
Any thoughts?
Active From | Cost | |
---|---|---|
Campaign 1 | 1/31/2024 | 0.5 |
Campaign 1 | 2/1/2025 | 0.6 |
Campaign 2 | 1/31/2024 | 0.5 |
Answers
-
This would be three tiles, a Join, a Rank & Window, and a Filter. Start by joining like you have, where every date joins to every campaign with an active date less than or equal to it. Then you just need to reduce that down to just the row with the greatest active date. In the Rank & Window, partition by a row identifier of some sort from the date table[1]. Sort by Active From descending. Then add the window function ROW_NUBMER, naming it
rank
. Then, in your filter tile, filter torank = 1
.
1.Raw Data.id
or something like that. If you don't have a row identifier of any kind inRaw Data
then the first thing you'd need to do is add one. Use Add Formula with the ROW_NUMBER() function.Randall Oveson <randall.oveson@domo.com>
0 -
I still want to have everything before 2/1 to be .5 and everything after to be .6. Then what if a 3rd date gets added?
0 -
I assume you mean you want everything before 1/31/2024 to be .5 and everything after 2/1/2025 to be .6. In other words, you want the earliest campaign to apply on into the past forever, and for the latest campaign to apply on forever into the future, or at least until another campaign is added with a later date?
The solution I've described already fulfills that second condition. Dates greater than the latest campaign will join to only it, and that campaign's cost will be the one selected by the Rank & Window → Filter. If another date is added that's later, but less than the date in a row ofRaw Data
, then it will join to that as well and that latestActive Date
will be the one to survive the Rank & Window → Filter instead.
To fulfill the first condition, I would just make it a left outer join, so that you get every row ofRaw Data
, even when theDate
is before any campaign'sActive From
. Those unjoined rows will have null forActive From
andCost
, so I would add a formula after the join to rewrite the Cost column, replacing those nulls in Cost with the desired before-all-campaigns value:
Add Formula
Cost:IFNULL(Cost, 0.5)
Randall Oveson <randall.oveson@domo.com>
0 -
It may be easier if I explain it like this:
First off, I'm assuming that yourCampaign
table has just one row for eachCampaign
, where theActive Date
is the date it begins. From your example, it seems like you might have two rows per Campaign, though, and I'm not sure how to interpret those. The first thing we need to do is change thatCampaign
table so that it only has one row perCampaign
, so we know what theCost
should be for each one. I'm not sure what we would do if we had multiple rows for a singleCampaign
with different costs. What does that mean?
If yourCampaign
table currently consists of one or two rows perCampaign
, and the second row, if present, represents the end of theCampaign
, and we can safely ignore theCost
from the end-of-campaign row, then you can just use aRemove Duplicates
tile using the campaign name or ID as the uniqueness column, and it will reduce the table down to the form we need, with just a row for the start date of the campaign.
1. We use Join to connect everyRaw Data
row to everyCampaign
row whereCampaign.Active Date
is beforeRaw Data.Date
. The result of this join will contain a mix of rows you want and rows you don't want.2. We use Rank & Window to rank the
Campaign
rows that eachRaw Data
row joined against. What we're trying to do is assign rank #1 to just one of the joined rows for eachRaw Data
row. The sort order will beActive Date
descending, so that rank #1 will be the most recentCampaign
that theRaw Data
joined against.
3. We use filter to select just cases whererank = 1
, bringing us back to just one row perRaw Data
row, with only the latest activeCampaign
with anActive Date
before ourDate
joined to it.Randall Oveson <randall.oveson@domo.com>
0 -
I do not really understand what is happening but it seems to be working. The one thing I do not want to do is filter out previous dates that had an older Cost.
0 -
Ok, if I take out the filter, it does not work how I would like it to. It is taking the old Cost and the new Cost and duplicating the rows to assign both of them.
0 -
If you have the Join → Rank & Window → Filter, and:
1. You're using left outer join withRaw Data
on the left
2. You have your Rank & Window partition by a unique ID column ofRaw Data
3. You're using the Row Number function in Rank & Window
4. Your Filter condition isrank
is equal to 1 (whererank
is the column containing the Row Number in Rank & Window)…then you should have exactly one result row for every
Raw Data
row. TheCampaign
info attached to the row will be determined by the join condition (which should beRaw Data.Date >= Campaign.Active From
) and your Rank & Window's sort order (which should beActive From
descending).
However, you may still have rows that have noCampaign
info on them. These are rows whoseDates
were not greater than any of theCampaign
dates. I don't know how you want to handle those, but you can do so using an Add Formula at the end, which overwrites theCost
and any other of theCampaign
columns that you may want.
If you want to attach the earliest Campaign to all the rows that are before any campaign, you can do that as well, but it's a little more complicated and will involve another join.Randall Oveson <randall.oveson@domo.com>
0
Categories
- All Categories
- 1.9K Product Ideas
- 1.9K Ideas Exchange
- 1.6K Connect
- 1.3K Connectors
- 303 Workbench
- 6 Cloud Amplifier
- 9 Federated
- 3K Transform
- 104 SQL DataFlows
- 640 Datasets
- 2.2K Magic ETL
- 4K Visualize
- 2.5K Charting
- 769 Beast Mode
- 72 App Studio
- 43 Variables
- 718 Automate
- 185 Apps
- 462 APIs & Domo Developer
- 57 Workflows
- 14 DomoAI
- 40 Predict
- 17 Jupyter Workspaces
- 23 R & Python Tiles
- 402 Distribute
- 116 Domo Everywhere
- 277 Scheduled Reports
- 9 Software Integrations
- 135 Manage
- 132 Governance & Security
- 8 Domo Community Gallery
- 44 Product Releases
- 12 Domo University
- 5.4K Community Forums
- 40 Getting Started
- 30 Community Member Introductions
- 111 Community Announcements
- 4.8K Archive