Objective:
I would like to add an IF condition to my query, but i'm not sure how. In other words, substitute the MAX expression for the IF statement below
The IF condition is:
IF(
'Data Model'[amount] = 'Data Model'[orderTotalPrice],
'Data Model'[amount] - 'Data Model'[shipping_price] - 'Data Model'[total_tax],
'Data Model'[amount]
)
My query:
VAR Summary =
CALCULATETABLE(SUMMARIZE (
'Data Model',
'Data Model'[orderId],
"MaxValue", CALCULATE (
MAX ( 'Data Model'[amount] ),
'Data Model'[kind] = "refund",
'Data Model'[status] = "success"
)
),USERELATIONSHIP ( 'Data Model'[trx_date], DateTable[Date] ))
RETURN
SUMX ( Summary, [MaxValue] )
Related
I have a table like this:
Name
Task
Active Task
Active User
Assigned Date
Completed date
CompletionPercentage
A
no1
TRUE
TRUE
11/09/2022
10/10/2022
100
A
no3
FALSE
TRUE
01/02/2020
02/04/2020
100
A
no2
TRUE
TRUE
05/10/2022
05/11/2022
100
B
no1
TRUE
TRUE
05/02/2022
06/10/2022
100
B
no2
TRUE
TRUE
04/07/2022
54.32098765
C
no2
TRUE
TRUE
10/12/2021
10/01/2022
100
C
no1
TRUE
TRUE
04/07/2022
04/08/2022
100
D
no2
TRUE
TRUE
02/15/2022
04/10/2022
100
D
TRUE
TRUE
04/03/2022
92.30769231
F
no1
TRUE
TRUE
05/10/2022
1.666666667
G
no2
TRUE
TRUE
53.08641975
H
no1
TRUE
TRUE
10/05/2022
100
I
no4
TRUE
TRUE
J
no4
TRUE
FALSE
11/05/2021
100
K
no3
FALSE
TRUE
I am trying to calculate if there are users who have more than 1 task assigned and more than 1 task completion:
Users with more than 1 task =
IF (
CALCULATE (
DISTINCTCOUNT ( 'Table'[Task] ),
DISTINCT ( 'Table'[Name] ),
'Table'[Active Task] = TRUE (),
'Table'[Active User] = TRUE (),
USERELATIONSHIP ( 'Table'[Assigned Date], 'Calendar'[Date] )
) > 1,
1
)
Users with more than 1 completion =
IF (
CALCULATE (
DISTINCTCOUNT ( 'Table'[Task] ),
DISTINCT ( 'Table'[Name] ),
'Table'[Active Task] = TRUE (),
'Table'[Active User] = TRUE (),
'Table'[CompletionPercentage] = 100,
USERELATIONSHIP ( 'Table'[Completed date], 'Calendar'[Date] )
) > 1,
1
)
With these measures however I am not able to get aggregates if I need to view, for example, by fiscal year:
Is there any way to get the sums of users who have multiple tasks and completions by year?
Thanks a lot for any help!
Later edit:
I used #Ozan-Sen 's formula and it works when viewing the data by Name or Country, but not when using Calendar Year or FY dimensions:
enter image description here
#new Users with more than 1 task = VAR Tbl = ADDCOLUMNS ( SUMMARIZE ( 'Table', 'Table'[Name] ), "Total_Task", CALCULATE ( DISTINCTCOUNT ( 'Table'[Task] ), 'Table'[Active Task] = TRUE (), 'Table'[Active User] = TRUE () ) ) RETURN COUNTX ( FILTER ( Tbl, [Total_Task] > 1 ), [Name] )
#new Users with more than 1 completion = VAR Tbl = ADDCOLUMNS ( SUMMARIZE ( 'Table', 'Table'[Name] ), "Total_Task", CALCULATE ( DISTINCTCOUNT ( 'Table'[Task] ), 'Table'[Active Task] = TRUE (), 'Table'[Active User] = TRUE (), 'Table'[CompletionPercentage] = 100, USERELATIONSHIP ( 'Table'[Completed date], 'Calendar'[Date] ) ) ) RETURN CALCULATE(COUNTX ( FILTER ( Tbl, [Total_Task] > 1 ), [Name] ), USERELATIONSHIP ( 'Table'[Completed date], 'Calendar'[Date] ))
In the pbix sample data here https://www.transfernow.net/dl/20221202bvPUEAuG, there are 3 users with multiple tasks in FY22 and 1 in FY23, but only the 3 ones in FY22 are displayed in the visual when adding FY. Likewise, there are 2 users with multiple tasks completed (1 in FY22 and 1 in FY23), but none are displayed with Calendar dimensions. I would like to have an accurate aggregate view by Calendar FY, can anyone help or explain what is the issue is?
Please try this solution:
I guess you have such a model:
Then Let's define measures:
Users with more than 1 task =
VAR Tbl =
ADDCOLUMNS (
SUMMARIZE ( YourTable, YourTable[Name] ),
"Total_Task",
CALCULATE (
DISTINCTCOUNT ( YourTable[Task] ),
'YourTable'[Active Task] = TRUE (),
'YourTable'[Active User] = TRUE ()
)
)
RETURN
COUNTX ( FILTER ( Tbl, [Total_Task] > 1 ), [Name] )
Users with more than 1 completion =
VAR Tbl =
ADDCOLUMNS (
SUMMARIZE ( YourTable, YourTable[Name] ),
"Total_Task",
CALCULATE (
DISTINCTCOUNT ( YourTable[Task] ),
'YourTable'[Active Task] = TRUE (),
'YourTable'[Active User] = TRUE (),
'YourTable'[CompletionPercentage] = 100,
USERELATIONSHIP ( YourTable[Completed date], 'Calendar'[Date] )
)
)
RETURN
COUNTX ( FILTER ( Tbl, [Total_Task] > 1 ), [Name] )
If we test it on a visual:
Update Requested from #Cristina:
#new Users with more than 1 task_BY_Calendar =
VAR IterateByFY = ADDCOLUMNS(
SUMMARIZE ( 'Table','Calendar'[FY]),
"Total_Task",
CALCULATE (
DISTINCTCOUNT ( 'Table'[Task] ),
'Table'[Active Task] = TRUE (),
'Table'[Active User] = TRUE ()
)
)
RETURN
SUMX(IterateByFY,[Total_Task])
My database schema looks like down below
ID
Date
Status
ID1
2022/01/01
Active
ID1
2022/02/01
Active
ID1
2022/03/01
Active
ID1
2022/04/01
Terminated
ID2
2022/01/01
Active
ID2
2022/02/01
Terminated
I'd like to calculate unique occurrences from start of selected date year, till the selected date. My formula is:
CountOfUnique = CALCULATE( DISTINCTCOUNT( 'Table'[ID] ) , 'Table'[STATUS] = "Active", DATESBETWEEN('CALENDAR'[DATE], STARTOFYEAR('CALENDAR'[DATE]), MAX('CALENDAR'[DATE]) ))
In SQL I'd need something like
SELECT COUNT ( DISTINCT ID) FROM Table
WHERE STATUS = "ACTIVE"
AND DATE BETWEEN 2022/01/01 AND 2022/04/01
Try this:
CountOfUnique =
CALCULATE (
DISTINCTCOUNT ( 'Table'[ID] ),
'Table'[STATUS] = "Active",
DATESBETWEEN (
'CALENDAR'[DATE],
STARTOFYEAR ( 'CALENDAR'[DATE] ),
SELECTEDVALUE ( 'CALENDAR'[DATE] )
)
)
when you have a slicer on the visual, the start of selected date year doesnt mean much as you select the dates on the slicer. I created a Calendar Table = CALENDARAUTO() so it started from the 2022/01/01...
use one of these as you like...
sample PBix File
Unique Count =
VAR _max =
MAX ( 'Calendar Table'[Date] )
VAR _min =
MIN ( 'Calendar Table'[Date] )
RETURN
CALCULATE (
DISTINCTCOUNT ( 'Table'[ID ] ),
'Table'[Date ] <= _max
&& 'Table'[Date ] >= _min
)
or only Active if you need
Unique Count (Active) =
VAR _max =
MAX ( 'Calendar Table'[Date] )
VAR _min =
MIN ( 'Calendar Table'[Date] )
RETURN
CALCULATE (
DISTINCTCOUNT ( 'Table'[ID ] ),
'Table'[Date ] <= _max
&& 'Table'[Date ] >= _min
&& 'Table'[Status] = "Active"
)
I have a table like below and trying to count the number of job_ids that are in "PAUSED" status in databricks and indicate it in PowerBI report.
If there is job id that has both of "PAUSED" and "UNPAUSED" status with the same timestamp, I should count that job_id as well as it's in the "PAUSED" status.
table1
job_id
status
timestamp
A
PAUSED
2022-08-02T21:09:13
A
UNPAUSED
2022-08-02T21:09:13
B
PAUSED
2022-08-10T21:09:15
B
PAUSED
2022-07-20T21:09:13
A
PAUSED
2022-08-12T21:09:13
C
PAUSED
2022-08-01T21:07:19
C
PAUSED
2022-08-01T21:07:19
C
UNPAUSED
2022-08-03T21:07:19
B
UNPAUSED
2022-07-20T21:09:13
A
UNPAUSED
2022-08-04T21:09:13
and this is the sql query I wrote
SELECT count(job_id) FROM (SELECT distinct job_id,status,DENSE_RANK over() partition
by job_id order by timestamp desc AS rn FROM table1) t2 WHERE
t2.rn = 1 and t2.status = "PAUSED"
What I want to do next is to display the number in PowerBI using the card visual.
To do that I think I need to convert the sql query above into DAX query to get the correct number..(not sure if there is better way to perform it, I'm new to PowerBI)
Anyone could help me with this query..?
Would appreciate any kind of help!
In Power BI you would need the following measure:
Paused Jobs =
CALCULATE(
DISTINCTCOUNT('Table'[job_id]),
'Table'[status] = "PAUSED"
)
You can then put this measure onto a Card visual next to your data table
I hope This is what you want :
If we test the summary table:
Paused_Jobs =
VAR SummaryTable =
ADDCOLUMNS (
CALCULATETABLE (
SUMMARIZE ( table1, table1[job_id], table1[status] ),
table1[status] = "PAUSED"
),
"Latest", CALCULATE ( VALUES ( table1[timestamp] ), LASTDATE ( table1[timestamp] ) ),
"TotalCount", CALCULATE ( COUNTROWS ( table1 ), LASTDATE ( table1[timestamp] ) )
)
VAR Result =
SUMX ( SummaryTable, [TotalCount] )
RETURN
SummaryTable
And the resulting screenshot:
You can check the result with sql query :
If It is what you want, and want it as a measure:
Paused_Jobs =
VAR SummaryTable =
ADDCOLUMNS (
CALCULATETABLE (
SUMMARIZE ( table1, table1[job_id], table1[status] ),
table1[status] = "PAUSED"
),
"Latest", CALCULATE ( VALUES ( table1[timestamp] ), LASTDATE ( table1[timestamp] ) ),
"TotalCount", CALCULATE ( COUNTROWS ( table1 ), LASTDATE ( table1[timestamp] ) )
)
VAR Result =
SUMX ( SummaryTable, [TotalCount] )
RETURN
Result
Source Data:
DAX:
Total Derived =
VAR selected_min_date =
MIN ( 'Date'[Date] )
VAR selected_max_date =
MAX ( 'Date'[Date] )
VAR vTable =
SUMMARIZE (
FILTER (
MyTable,
MyTable[PDate] >= selected_min_date
&& MyTable[PDate] <= selected_max_date
),
MyTable[Gcode],
MyTable[DerTax]
)
var result = sumx(vTable, MyTable[DerTax])
RETURN
Result
I was expecting result as 2644.48, but is outputting 1322.24
What am doing wrong.
I was same result same as with below sql.
Select GCode,
sum(DerTax)
from MyTable
where PDate >= #datemin and PDate <=#datemax
group by GCode
I don't think you've grasped how SUMMARIZE works. Since your Gcode and DerTax entries are identical for each of the two rows, your SUMMARIZE statement will generate a table comprising just a single row, viz:
Gcode
DerTax
Grp01
1322.24
Instead of passing DerTax as a GroupBy column, you should be passing an aggregation of that field:
Total Derived: =
VAR selected_min_date =
MIN( 'Date'[Date] )
VAR selected_max_date =
MAX( 'Date'[Date] )
VAR vTable =
SUMMARIZE(
FILTER(
MyTable,
MyTable[PDate] >= selected_min_date
&& MyTable[PDate] <= selected_max_date
),
MyTable[Gcode],
"Tax", SUM( MyTable[DerTax] )
)
VAR result =
SUMX(
vTable,
[Tax]
)
RETURN
Result
I am trying to create a matrix table that will count the number of jobs due in a month and also if they are completed on time.
At present I can get it this far.
I have tried summarizing data, creating measures etc. But to no avail.
I need to produce the following:
My data source is a series of transactions lines that include
1) Transaction Due date
2) Transaction completed date
Definition of terms:
Due in month Count of due date
Done on time Completed within due month
Outside time completed outside due month
"%Perf" percentage completed on time.
Any help with this would be much appreciated.
Add the following measures:
Measure "Due":
Due = COUNTROWS ( Table1 )
Measure "Done on time":
Done on time =
VAR DueMonth = MONTH ( FIRSTDATE ( Table1[due_date] ) )
RETURN
CALCULATE (
[Due],
FILTER (
Table1,
MONTH ( Table1[completed] ) = DueMonth
)
)
Measure "Outside time":
Outside time =
VAR DueMonth = MONTH ( FIRSTDATE ( Table1[due_date] ) )
RETURN
CALCULATE (
[Due],
FILTER (
Table1,
MONTH ( Table1[completed] ) <> DueMonth &&
NOT ISBLANK ( Table1[completed] )
)
)
Measure "Incomplete":
Incomplete =
VAR DueMonth = MONTH ( FIRSTDATE ( Table1[due_date] ) )
RETURN
CALCULATE (
[Due],
FILTER (
Table1,
ISBLANK ( Table1[completed] )
)
)
Measure "% Perf"
% Perf =
DIVIDE (
[Done on time],
[Due],
BLANK()
)
Group by due_date.month in report.
See https://pwrbi.com/so_55513978/ for worked example PBIX file.
EDIT after updated question in comment:
There are many ways you could approach creating a visualisation which combines these measures, with the monthly counts by completion date, if you really must. Here's one approach:
Create a "Report Columns" table:
Report Columns =
VAR ListMeasures =
DATATABLE (
"Header", STRING,
"Sort Order", INTEGER,
"First Of Month", DATETIME,
{
{"Due in month", 1, BLANK() },
{"Done on time", 2, BLANK() },
{"Outside time", 3, BLANK() },
{"Incomplete", 4, BLANK() },
{"% Perf", 5, BLANK() }
}
)
VAR ListMonths =
CALCULATETABLE(
GROUPBY (
ADDCOLUMNS (
DISTINCT ( Table1[completed] ),
"Header", FORMAT ( Table1[completed], "YYYY-MMM" ),
"Sort Order", YEAR ( Table1[completed] ) * 100 + MONTH ( Table1[completed] ),
"First Of Month", DATE ( YEAR ( Table1[completed] ), MONTH ( Table1[completed] ), 1 )
),
[Header], [Sort Order], [First Of Month]
),
Table1[completed] <> BLANK()
)
RETURN
UNION (
ListMeasures,
ListMonths
)
Set column Header to Sort By column Sort Order
Create Measure "Report Measure":
Report Measure =
IF (
NOT HASONEFILTER ( 'Report Columns'[Header] ),
BLANK(),
SWITCH (
VALUES ( 'Report Columns'[Header] ),
"Due in month", [Due],
"Done on time", [Done on time],
"Outside time", [Outside time],
"Incomplete", [Incomplete],
"% Perf", [% Perf],
CALCULATE (
[Due],
FILTER (
Table1,
DATE ( YEAR ( Table1[completed] ), MONTH ( Table1[completed] ), 1 ) = VALUES ( 'Report Columns'[First Of Month] )
)
)
)
)
Add a matrix visualisation, with Table1[due_date] in rows, Report Columns[Header] in Columns, and [Report Measure] in values. Format to suit.
Updated example PBIX file: https://pwrbi.com/so_55513978-2/