how to Find out users_id with some conditions filter

孤人 提交于 2020-12-15 05:15:42

问题


i have a table like this

order_id | user_id | createdAt | transaction_amount

order_id as the id of the transaction, user_id as the user, createdAt as the dates, and transaction_amount is the transaction of each id order.

so on tableau i want to find out users in time range '2020-01-01' until '2020-01-31' with 2 conditions

  1. the users are doing transaction before last date in range ('2020-01-31') and atleast doing more than 1 transaction
  2. and the users are at least doing 1 transaction in date range ('2020-01-01' until '2020-01-31')

on mysql that conditions can be described with this query

HAVING SUM(createdAt <= '2020-01-31') > 1
           AND SUM(createdAt BETWEEN '2020-01-01' AND '2020-01-31')

on tableau i did this

[![enter image description here][1]][1]

on first filter (createdAt) i made range of dates ('2020-01-01' until '2020-01-31') on second filter (createdAt copy) i made range before last range ( < '2020-01-31') on filter CNTD(user_id) i made count distinct at least 1.

so it appear 2223 users, instead when i check it in mysql, its appear 1801 user, and the mysql was always right since i used mysql and im new in tableau. so what did i missed in here?


回答1:


Edit: Take this example

  • user1 doing 2 transactions, 1 each in Dec-19 and Jan-20
  • user2 doing 1 transaction in Jan-20
  • user3 doing 2 transactions in Dec-19
  • user4 doing 2 transactions in Feb-20
  • user5 doing 2 transactions in Jan-20

data snapshot

Now if your date range is Jan-20 (say). If you want users doing at least two transactions before end of date range (31 Jan 2020) and at least one of these should be in Jan-2020 then user1 and user5 satisfy the condition.

In this case proceed like this-

Step-1 Create two date type parameters (parameter 1 and parameter 2 respectively for start and end of date ranges)

Step-2 create a calculated field condition with calculation as

{Fixed [User]: sum(
if [Trans Date]<=[Parameter 2] then 1 else 0 end)}>=2
AND
{FIXED [User]: sum(
IF [Trans Date]<=[Parameter 2] AND 
[Trans Date] >= [Parameter 1] THEN 1 ELSE 0 END)}>=1

this will evaluate to TRUE whenever your given condition is satisfied. See this screenshot.

Needless to say trans date is your createdAt



来源:https://stackoverflow.com/questions/64712691/how-to-find-out-users-id-with-some-conditions-filter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!