My data:
week
sale
20
50
20
20
21
10
21
10
22
5
22
5
Desired result:
week
sale
change
20
50
any
20
20
any
21
10
-50
21
10
-50
22
5
-10
22
5
-10
Where:
Week 20 total = 70,
Week 21 total = 20,
Week 22 total = 10
Diff for week 21 = 20-70 = -50
Diff for week 22 = 10-20 = -10
Note: "any" = can be anything (i.e. 0 or null)
Calculated column
Column 2 =
VAR _1 =
CALCULATE ( MAX ( 'Table'[week] ) ) - 1
VAR _2 =
SUMX ( FILTER ( ALL ( 'Table' ), 'Table'[week] = _1 ), 'Table'[sale] )
VAR _3 =
CALCULATE ( SUM ( 'Table'[sale] ), ALLEXCEPT ( 'Table', 'Table'[week] ) )
VAR _4 =
IF ( ISBLANK ( _2 ) = TRUE (), BLANK (), _3 )
RETURN
_4
Measure
currentWeekTotal:= SUM('Table'[sale])
prevWeekTotal:=
VAR _1 = MAX('Table'[week])-1
VAR _2 = SUMX(FILTER(ALL('Table'),'Table'[week]=_1),'Table'[sale])
RETURN _2
change:= IF([prevWeekTotal]=BLANK(),BLANK(),[currentWeekTotal]-[prevWeekTotal])
Here is a simple code for a calculated column :
change =
VAR _firstweek = MIN ( 'Table'[week] )
VAR _thisweek = 'Table'[week]
VAR _thisweeksales = CALCULATE ( SUM ( 'Table'[sale] ) , ALLEXCEPT ( 'Table' , 'Table'[week] ) )
VAR _lastweeksales = CALCULATE ( SUM ( 'Table'[sale] ) , ALL ( 'Table' ) , 'Table'[week] = _thisweek - 1 )
VAR _diff = IF ( _thisweek <> _firstweek , _thisweeksales - _lastweeksales )
RETURN _diff
This also handles skipped weeks that are not the first week.
Related
I have a table like this:
DeviceID
SignalID
Value
DateTime
1
22
1032223
01.10.2022 00:00:00
2
24
1923892
01.10.2022 00:00:00
3
33
3434342
01.10.2022 00:00:00
4
33
3232323
01.10.2022 00:00:00
.......
......
......
......
1
33
4155151
05.12.2022 15:36:38
I have a few devices with each device monitoring signals. The signal value here represents some counter value. The counter are running continously but I need their relative value as in from 01.10.2022.
What I want to calculate :
The value of the signalID x of device y = (value now) - (value on 01.10.2022 00:00:00).
How can I acheive this for all the value of the signalID of all the devices?
You can test this, and let me know if It solves your problem!
15Seconds_Cardinality =
VAR NowTime =
NOW ()
VAR FixedTime =
DATE ( 2022, 10, 01 ) + TIME ( 0, 0, 0 )
VAR Value_On_FixedDate =
CALCULATE (
MIN ( Signals[Value] ),
YEAR ( Signals[DateTime] ) = YEAR ( FixedTime ),
MONTH ( Signals[DateTime] ) = MONTH ( FixedTime ),
DAY ( Signals[DateTime] ) = DAY ( FixedTime ),
HOUR ( Signals[DateTime] ) = HOUR ( FixedTime ),
MINUTE ( Signals[DateTime] ) = MINUTE ( FixedTime ),
ROUND ( SECOND ( Signals[DateTime] ) / 15, 0 )
= ROUND ( SECOND ( FixedTime ) / 15, 0 )
)
VAR Value_On_Now =
CALCULATE (
MAX ( Signals[Value] ),
YEAR ( Signals[DateTime] ) = YEAR ( NowTime ),
MONTH ( Signals[DateTime] ) = MONTH ( NowTime ),
DAY ( Signals[DateTime] ) = DAY ( NowTime ),
HOUR ( Signals[DateTime] ) = HOUR ( NowTime ),
MINUTE ( Signals[DateTime] ) = MINUTE ( NowTime ),
ROUND ( SECOND ( Signals[DateTime] ) / 15, 0 )
= ROUND ( SECOND ( NowTime ) / 15, 0 )
)
RETURN
Value_On_Now - Value_On_FixedDate
Same above code with image file:
I have a measure:
_Forecast, % =
VAR ForecastMinimumPercent = 10
VAR ForecastIncreasePercent = 15
VAR sM =
MAX ( T1[PlanDate] ) //Plan Date
VAR fM =
MAX ( '_Date2'[Date] ) //fact Date
VAR sC =
FIRSTNONBLANK ( T1[ProjectCode], 1 ) //Current project code
VAR tP =
//Current forecast cumulative percent
CALCULATE (
SUM ( T1[ForecastPercent] ),
FILTER (
ALLSELECTED ( 'T1' ),
T1[PlanDate] <= fM
&& T1[PlanDate] <= sM
&& T1[ProjectCode] = sC
),
REMOVEFILTERS ( T1[PlanDate] )
)
VAR mM =
// Current Project first Date
CALCULATE (
MIN ( T1[PlanDate] ),
FILTER ( ALLSELECTED ( 'T1' ), T1[ProjectCode] = sC ),
REMOVEFILTERS ( T1[PlanDate] )
)
VAR nD =
ROUNDUP ( DIVIDE ( 100 - tP, ForecastIncreasePercent, 0 ), 0 ) // count of forecast columns
VAR fD =
DATEDIFF ( sM, fM, MONTH ) //dates offsets, '_Date2'[Date] - T1[PlanDate]
VAR P1 = tP + ForecastIncreasePercent * fD //forecasted percent
VAR P2 =
IF ( P1 > 100, 100, P1 ) //does not show values over 100, for example 90+15=105 but show 100
VAR fP =
SWITCH (
TRUE (),
tP < ForecastMinimumPercent, BLANK (),
// if percent <10, then hide
fM < mM, BLANK (),
//hide values if T1[PlanDate] <> '_Date2'[Date]
MAX ( T1[ForecastPercent] ) = 0, BLANK (),
// hide empty percent rows
fM
> DATE ( YEAR ( sM ), MONTH ( sM ) + nD + 1, DAY ( sM ) ), BLANK (),
//does not show 100 more than once
fM <= sM, IF ( HASONEVALUE ( T1[PlanDate] ), BLANK (), tP ),
//if collapsed show first fact values
P2
)
RETURN
fP
This measure give me current forecast percent
But i need get difference current month - previous
How to write right syntax?
i have simple dataset like below where the delta is a measure (not a column) which is the diff of demand and supply.I wanted to calculated the running sum of measure "delta" as shown below.running sum needs to be at material-location-week level.
dax i tried:
Cum =
var mat = MAX('table'[Material])
var pla = MAX('table'[Plant])
var pw =MAX('table'[PWk])
return
CALCULATE(
table[delta],
FILTER(
ALL(table),
'table'[Material]= mat && 'table'[Plant] <= pla && 'table'[PWk]<=pw
)
))
<>
material location week demand supply delta(demand-supply) running_sum??
123 1000 wk1 100 40 60 60
123 1000 wk2 40 30 10 70
123 2000 wk1 30 20 10 10
123 2000 wk2 40 15 25 35
please help. I am stuck with this and dont know`enter code here` where i am going wrong.
Perhaps:
running_sum =
VAR Mat =
MAX ( 'Table'[material] )
VAR Loc =
MAX ( 'Table'[location] )
VAR Wk =
MAX ( 'Table'[week] )
RETURN
CALCULATE (
[delta],
FILTER (
ALL ( 'Table' ),
'Table'[material] = Mat
&& 'Table'[location] = Loc
&& 0 + SUBSTITUTE ( 'Table'[week], "wk", "" )
<= 0 + SUBSTITUTE ( Wk, "wk", "" )
)
)
How can I write a measure to count the number of userID for which sum(x1) is equal to count(order_id), in Power BI?
For example, my data table is:
userID
x1
order_id
141
1
719
172
0
616
172
0
189
172
0
2211
172
0
317
1103
1
98
1103
1
213
1103
1
15
2524
0
4902
2524
1
3620
and I use table visual of power bi for this, to explain my mean:
userID
sum(x1)
count(order_id)
141
1
1
172
0
4
1103
3
3
2524
1
2
Note that the userID column is one of the columns in my data table, and calculating sum(x1) and count(order_id) in this sample is by Power BI default features.
The result for this sample should be 2. I need a measure that returns 2.
Measure1 =
VAR _base1 =
SUMMARIZE ( 'Table 1', 'Table 1'[userID] )
VAR _base2 =
ALLEXCEPT ( 'Table 1', 'Table 1'[userID] )
VAR _ct =
ADDCOLUMNS ( _base1, "X", CALCULATE ( COUNT ( 'Table 1'[order_id] ), _base2 ) )
VAR _sum =
ADDCOLUMNS ( _base1, "X", CALCULATE ( SUM ( 'Table 1'[x1] ), _base2 ) )
VAR _nt =
NATURALINNERJOIN ( _sum, _ct )
RETURN
COUNTROWS ( _nt )
or
Measure4 =
VAR _1 =
COUNTX (
VALUES ( 'Table 1'[userID] ),
VAR _base =
ALLEXCEPT ( 'Table 1', 'Table 1'[userID] )
VAR _1 =
CALCULATE ( SUM ( 'Table 1'[x1] ), _base )
VAR _2 =
CALCULATE ( COUNTROWS ( 'Table 1' ), _base )
VAR _3 =
IF ( _1 = _2, 1 )
RETURN
_3
)
RETURN
_1
This should work
count_valid_rows =
VAR sum_x1_table =
SUMMARIZECOLUMNS ( 'table'[userID], 'table', "sumx1", SUM ( 'table'[x1] ) )
VAR count_orderId_table =
SUMMARIZECOLUMNS (
'table'[userID],
'table',
"countOfOrders", COUNT ( 'table'[x1] )
)
RETURN
COUNTROWS (
FILTER (
NATURALINNERJOIN ( sum_x1_table, count_orderId_table ),
[sumx1] = [countOfOrders]
)
)
Docs of the functions used.
NATURALINNERJOIN
SUMMARIZECOLUMNS
Another suggestion:
Count :=
SUMX (
SUMMARIZECOLUMNS (
'Table'[userID] ,
"Sum" , SUM ( 'Table'[x1] ),
"Count" , COUNT ( 'Table'[order_id] )
),
IF ( [Sum] = [Count] , 1 )
)
As you see from the other answers there are heaps of ways to calculate this. I suggest you look over all the suggestions to understand what is going on in each, and then write out your preferred way of dealing with this type of issue after.
Your new measure may looks like this one:
calculate( countrows('YourTabel'), FILTER(ALL('YourTabel'), somestatementIfneeded && var __x1 = [x1] var __x2 = [x2] return __x1 = __x2))
The main part is to use variable PLACEHOLDER;
I have data like this,
App_Num Days Price
A1 10 100
A1 11 150
A2 11 200
A3 12 250
A3 12 300
A4 20 350
A4 21 400
The average of the days is displayed on a card visual as 13.857.
Now, there are two parameters that are set for user to adjust the values and see.
Total Value (Min and Max Range)
Days
For example, if the user selects 0-280- it is expected to list A1 (100 + 150 = 250 less than 280) and A2 (200 being less than 280).
I used a DAX like this and built a table like this,
Apps_in_scope =
Var min_amount = Min('Total Value'[Total Value])
Var max_amount = Max('Total Value'[Total Value])
var required_app_num = SELECTEDVALUE(Table1[App_Num])
Var required_amount = CALCULATE(sum(Table1[Price]),FILTER(Table1,Table1[App_Num] = required_app_num))
var in_scope = if(And(required_amount <= max_amount, required_amount >= min_amount),1,0)
return in_scope
And I was able to produce a Visual like this,
App_Num Apps_in_scope
A1 1
A2 1
A3 0
A4 0
Now after selecting the total price range, if the user selects the days parameter manually to be 15 then my average will shift as per this logic.
A1 has 2 transactions and with in the selected price range of 280 will become (15*2)
A2 has 1 transaction and with in the selected price range of 280 become (15*1)
A3 has 2 transaction and will remain unchanged (12+12)
A4 has 2 transactions and will remain unchanged (20+21)
So my new measure which I want to place on the card is expected to show now (15+15+15+12+12+20+21)/7 = 15.714
How can I write this measure. Kindly help me with this
I'd tweak your measure slightly so that it works better for taking the average:
Apps_in_scope_2 =
VAR min_amount = MIN ( 'Total Value'[Total Value] )
VAR max_amount = MAX ( 'Total Value'[Total Value] )
VAR required_amount =
CALCULATE ( SUM ( Table1[Price] ), ALLEXCEPT ( Table1, Table1[App_Num] ) )
VAR in_scope =
IF ( AND ( required_amount <= max_amount, required_amount >= min_amount ), 1, 0 )
RETURN
in_scope
With this tweak the average is fairly simple:
AvgMeasure =
VAR DaysParam = SELECTEDVALUE ( DaysSlicer[Days] )
RETURN
AVERAGEX( Table1, IF( [Apps_in_scope_2] = 1, DaysParam, Table1[Days] ) )
Edit:
Here's an alternative version that doesn't use the first measure but should scale better to large data tables.
AvgAlternate =
VAR min_amount = MIN ( 'Total Value'[Total Value] )
VAR max_amount = MAX ( 'Total Value'[Total Value] )
VAR DaysParam = SELECTEDVALUE ( DaysSlicer[Days] )
VAR apps =
ADDCOLUMNS (
SUMMARIZE (
Table1,
Table1[App_Num],
"#Price", SUM ( Table1[Price] ),
"#Rows", COUNT ( Table1[Price] )
),
"#Days",
IF (
AND ( [#Price] <= max_amount, [#Price] >= min_amount ),
DaysParam * [#Rows],
CALCULATE ( SUM ( Table1[Days] ) )
)
)
RETURN
DIVIDE ( SUMX ( apps, [#Days] ), SUMX ( apps, [#Rows] ) )
This is assuming that you have separate tables for your Price range and Days selection (as in what-if parameter tables).
My measure =
VAR apps =
SELECTCOLUMNS (
FILTER (
SUMMARIZE ( Table1, Table1[App_Num], "Total Price", SUM ( Table1[Price] ) ),
[Total Price] >= MIN ( 'Total Value'[Total Value] )
&& [Total Price] <= MAX ( 'Total Value'[Total Value] )
),
"App_Num", [App_Num]
)
RETURN
AVERAGEX (
Table1,
IF ( Table1[App_Num] IN apps, SELECTEDVALUE ( Days[Days] ), Table1[Days] )
)