在Tableau中基于多数据源时间区间过滤时序数据
Got it, let's tackle this problem step by step—you're trying to filter rows from Source 1 based on the time intervals defined by Source 2's start/stop events in Tableau, right? Here are two reliable methods using Tableau Prep and Tableau Desktop that should get you the exact output you need:
Method 1: Using Tableau Prep (Best for Preprocessing Data)
Tableau Prep is perfect for this kind of ETL-style filtering since it lets you shape and join data easily:
- Import both data sources
Drag your Source 1 (numeric data) and Source 2 (event timestamps) into the Prep canvas. - Transform Source 2 into time intervals
- First, add a Calculated Field to Source 2 named
Interval Groupwith this formula:
This groups eachRUNNING_SUM(IF [event] = 'start' THEN 1 ELSE 0 END)startand its correspondingstopinto the same interval ID. - Next, use the Aggregate tool on Source 2:
- Group by
Interval Group - Create two aggregated fields:
start_time:MIN(IF [event] = 'start' THEN [timestamp] END)end_time:MIN(IF [event] = 'stop' THEN [timestamp] END)
You’ll now have a clean table of valid time intervals.
- Group by
- First, add a Calculated Field to Source 2 named
- Join and filter
- Drag the transformed Source 2 (interval table) onto Source 1 to create a Cross Join (select Cross Join from the join type dropdown).
- Add a Filter tool, set the condition to:
[Source 1.timestamp] >= [start_time] AND [Source 1.timestamp] <= [end_time]
- Clean up and export
Remove any extra fields (likeInterval Group,start_time,end_time) if you don’t need them, then output the filtered Source 1 data—this will match your desired result exactly.
Method 2: Using Tableau Desktop (For In-Visualization Filtering)
If you need to do this directly in Desktop while building a view, here’s how:
- Connect to both data sources
Add Source 1 and Source 2 to your Desktop data source. If they’re separate files (like CSVs), they’ll show up as two tables in the Data Source tab. - Generate intervals with Custom SQL
Right-click Source 2 in the Data Source tab, select Convert to Custom SQL, and replace the default query with this:
This query pairs eachWITH ranked_events AS ( SELECT event, timestamp, ROW_NUMBER() OVER (ORDER BY timestamp) AS row_num FROM [Source 2] ) SELECT r1.timestamp AS start_time, r2.timestamp AS end_time FROM ranked_events r1 JOIN ranked_events r2 ON r1.row_num = r2.row_num - 1 WHERE r1.event = 'start' AND r2.event = 'stop'startwith the nextstopto create valid intervals. - Join and filter
- Drag the custom SQL interval table onto Source 1 to create a Cross Join.
- Create a Calculated Field named
In Valid Interval?with:[Source 1.timestamp] >= [start_time] AND [Source 1.timestamp] <= [end_time] - Drag
In Valid Interval?to the Filters pane and select True.
- View the results
Drag Source 1’sx,y,z, andtimestampfields into your view—you’ll only see rows that fall within the intervals from Source 2.
Quick Notes
- Make sure both
timestampfields are the same data type (in your case, they’re both Unix timestamps/integers, so this is already handled). - If your Source 2 ever has non-alternating events (e.g., two starts in a row), you’ll need to adjust the grouping logic, but your sample data is perfectly structured for these methods.
内容的提问来源于stack exchange,提问作者7ball
相关产品推荐
相关产品推荐

