power BI Taking MIN and Max date by comparing two tables - report

I have three table namely Allitems, InItems and OutItems. The main table is Allitems which is related to the other two table with IN_OUT_ID.
I have been able to calculate the min and max date based on the IDs for each of the
Dataset
table using the below DAX Formula
For InItems tables
Min_Start_Date = CALCULATE( MIN(InItems[InDate]), ALLEXCEPT( InItems, InItems[IN_ID] ))
Max_end_Date = CALCULATE( MAX(InItems[InDate]), ALLEXCEPT( InItems, InItems[IN_ID] ))
For OutItems table
Min_Start_Date = CALCULATE( MIN(OutItems[OutDate]), ALLEXCEPT( OutItems, OutItems[OUT_ID]))
Max_end_Date = CALCULATE( MAX(OutItems[OutDate]), ALLEXCEPT( OutItems, OutItems[OUT_ID] ))
the relationship i have change the direction to flow both ways
I am trying to bringback min and max date by comparing the the min and max date from OutItems and Allitems and use it as a column in Allitems table. For example taking IN_ID 1, the Min date is 08/01/2019. I will also take the min date of OUT_ID which is 03/02/2019. Then I want the min date between these two date which is 08/01/2019.
Can anybody help on how I can achieve this
Expected outcome
I am open to any question thanks

To do this, you don't need relationships to filter both ways and you don't need those four calculated columns. Just take the min/max of the min/max:
Mindate =
MIN (
CALCULATE ( MIN ( InItems[InDate] ) ),
CALCULATE ( MIN ( OutItems[OutDate] ) )
)
and
Maxdate =
MAX (
CALCULATE ( MAX ( InItems[InDate] ) ),
CALCULATE ( MAX ( OutItems[OutDate] ) )
)
Note that the CALCULATE here performs a context transition that applies the row context (the IN_OUT_ID and Item values in the current row) as a filter context when taking the min/max over the other tables. If you remove it, you'd get the min/max across all ID values.

Related

Getting a column with count from a nested query

as a newbie in SQL I am lost regarding nested queries.
I am trying to achieve the following: getting a table grouped by month with a count from all values of a column, then the same count filtered by status.
So for instance in January I could have the following result:
Jan 22 Count= 100 Count with Status filter= 57
I tried several variations along these lines:
SELECT
FORMAT ( [CreatedDate.table2] , 'yyyyMM' ) as create_month,
RecordTypeName__c,
count(*) as count_all,
count_filtered
FROM
(SELECT
FORMAT ( [CreatedDate] , 'yyyyMM' ) as create_month,
RecordTypeName__c,
Count(*) AS count_filtered
FROM DM_AccessNoAgg.DimLead
WHERE [CreatedDate] >= '2022-01-01'
AND [Status]='Qualifiziert'
GROUP BY RecordTypeName__c,FORMAT ( [CreatedDate] , 'yyyyMM' )
)
Basically I am using the same value in both cases, just that the second count has to be filtered. What's the best method to get this done?
Thanks for your help!
Pauline.

Invalid OADate when using SUMX on a Date field from a Date Table

Thanks for reading,
I have a DAX measure that finds the slope of some data over time. To do this it needs to find the SUMX of a date field from a date table.
When I try to display only this sum, it comes back with an invalid OADate value error message:
Couldn't load the data for this visual
Invalid OADate value '7126785'. Accepted values are between -657435.0 and 2958465.99999999
Here is the query I'm using:
measure =
VAR Known =
FILTER (
SELECTCOLUMNS (
ALLSELECTED ( 'Dates'[Date] ),
"Known[X]", 'Dates'[Date],
"Known[Y]", sum('table'[peoplecount])
),
AND (
NOT or(( ISBLANK ( Known[X] ) ),Known[X] = 0),
NOT or(( ISBLANK ( Known[Y] ) ),Known[Y] = 0)
)
)
VAR Count_Items =
COUNTROWS ( Known )
VAR Sum_X =
SUMX(Known,Known[X])
RETURN
Sum_X
My date table looks like this:
Date (Date)
3/22/2020
3/23/2020
etc
My table containing data to be counted looks like this :
(This comes from direct query)
Date (Datetime),peoplecount (int)
3/21/2020 12:00:00 AM, 3
3/22/2020 12:00:00 AM, 9
etc

Earliest Time in Datetime column PowerBI

Okay so I have a table like shown...
I want to use PowerBI to create a new column called 'First_Interaction' where it will say 'True' if this was the user's earliest entry for that day. Any entry that came in after the first entry will be set to "False".
This is what I want the column to be like...
Use the following DAX formula to create a column:
First_Interaction =
VAR __userName = 'Table'[UserName]
VAR __minDate = CALCULATE( MIN( 'Table'[Datetime] ), FILTER( 'Table', 'Table'[UserName] = __userName ) )
Return IF( 'Table'[Datetime] = __minDate, "TRUE", "FALSE" )
Power BI dosnt support less than second so your DateTime Column must be a Text value. Take that on consideration for future transformation.

Shiny: BigQuery Fails when user selects "All" value

I am trying to use a BigQuery query to populate plots in Shiny. The query includes input values from the ui using selectInput. If the user selects a value that exists in the DB, such as year is 2014, the query works correctly, however, I would like the user to also be able to select "All." "All" should be a selection of all values, however, I am not sure how to express that in the query using selectInput.
server.r
data1 <- eventReactive(input$do_sql, {
bqr_auth(token = NULL, new_user = FALSE, verbose = FALSE)
query = paste('select month, event, partner_name, sum(f0_) from [dataset.table] where year =',input$year1,' and partner_name = \"',input$partner_name,'\"
GROUP by 1,2,3
ORDER by 1 asc
LIMIT 10000', sep="")
bqr_query(projectId, datasetId, query, maxResults =2000)
})
ui.r
(
selectInput("year1",
"Year:",
c("All",2014,2015
))
),
(
selectInput("partner_name",
"Partner:",
c("All",
unique(as.character(data5$partner_name))))
You should slightly change the query you are constructing
So, currently you have
SELECT month, event, partner_name, SUM(f0_)
FROM [dataset.table]
WHERE year = selected_year
AND partner_name = "selected_partner_name"
GROUP BY 1,2,3
ORDER BY 1 ASC
LIMIT 10000
with respectively:
selected_year --> input$year1
selected_partner_name --> input$partner_name
Instead, you should construct below query
SELECT month, event, partner_name, SUM(f0_)
FROM [dataset.table]
WHERE (year = selected_year OR "selected_year" = "All")
AND (partner_name = "selected_partner_name" OR "selected_partner_name" = "All")
GROUP BY 1,2,3
ORDER BY 1 ASC
LIMIT 10000
I am not shiny user at all - so excuse my syntax - below is just my
guess with regard of implementing above suggestion
query = paste('SELECT month, event, partner_name, sum(f0_)
FROM [dataset.table]
WHERE (year =',input$year1,' OR "All" ="',input$year1,'")
AND (partner_name = \"',input$partner_name,'\" OR "All" = \"',input$partner_name,'\")
GROUP by 1,2,3
ORDER by 1 asc
LIMIT 10000', sep="")
Mikhail's solution worked perfectly for character variables, but numerics didn't work correctly. I decided to use a character date range instead of the year numeric I originally used. Thanks.

Preventing Max function from using timestamp as part of criteria on a date column in PL/SQL

If I query:
select max(date_created) date_created
on a datefield in PL/SQL (Oracle 11g), and there are records that were created on the same date but at different times, Max() returns only the latest times on that date. What I would like to do is have the times be ignored and return ALL records that match the max date, regardless of their associated timestamp in that column. What is the best practice for doing this?
Edit: what I'm looking to do is return all records for the most recent date that matches my criteria, regardless of varying timestamps for that day. Below is what I'm doing now and it only returns records from the latest date AND time on that date.
SELECT r."ID",
r."DATE_CREATED"
FROM schema.survey_response r
JOIN
(SELECT S.CUSTOMERID ,
MAX (S.DATE_CREATED) date_created
FROM schema.SURVEY_RESPONSE s
WHERE S.CATEGORY IN ('Yellow', 'Blue','Green')
GROUP BY CUSTOMERID
) recs
ON R.CUSTOMERID = recs.CUSTOMERID
AND R.DATE_CREATED = recs.date_created
WHERE R.CATEGORY IN ('Yellow', 'Blue','Green')
Final Edit: Got it working via the query below.
SELECT r."ID",
r."DATE_CREATED"
FROM schema.survey_response r
JOIN
(SELECT S.CUSTOMERID ,
MAX (trunc(S.DATE_CREATED)) date_created
FROM schema.SURVEY_RESPONSE s
WHERE S.CATEGORY IN ('Yellow', 'Blue','Green')
GROUP BY CUSTOMERID
) recs
ON R.CUSTOMERID = recs.CUSTOMERID
AND trunc(R.DATE_CREATED) = recs.date_created
WHERE R.CATEGORY IN ('Yellow', 'Blue','Green')
In Oracle, you can get the latest date ignoring the time
SELECT max( trunc( date_created ) ) date_created
FROM your_table
You can get all rows that have the latest date ignoring the time in a couple of ways. Using analytic functions (preferrable)
SELECT *
FROM (SELECT a.*,
rank() over (order by trunc(date_created) desc) rnk
FROM your_table a)
WHERE rnk = 1
or the more conventional but less efficient
SELECT *
FROM your_table
WHERE trunc(date_created) = (SELECT max( trunc(date_created) )
FROM your_table)

Resources