acc.num
Balance
Period
1234
200
1
2345
300
1
1234
300
2
2345
200
2
Is there a way to do row level variances of the balance for each acc. num by period with dax?
I've tried separating the single table in too multiple by filtering periods but can't figure out how to do it by row
Desired output would be the difference in period by account for example 1234 period 2 Balance 300 minus period 1 balance 200 = 100(result im looking for)
This should do the trick. Note that the first balance will be equal to the amount of the first period.
MyVariance =
VAR currentBalance =
SUM ( MyTable[Balance] )
VAR priorBalance =
CALCULATE (
SUM ( MyTable[Balance] ),
REMOVEFILTERS ( MyTable[Period] ),
MyTable[Period]
= MAX ( MyTable[Period] ) - 1
)
RETURN
currentBalance - priorBalance
Related
I am reading a signal value every few seconds.Plotting the signal results in a clipped fullwave rectified sine wave.I am trying to write a measure, such that everytime the signal completes a period, I should update the counter by 1.How can I do it?
This is how the table looks. This table shows the completion of 1 period.
DeviceID
TesterID
Value
DateTime
1
22
0
01.10.2022 00:00:00
1
22
500
01.10.2022 00:01:00
1
22
500
01.10.2022 00:01:15
1
22
1000
01.10.2022 00:02:00
1
22
1000
01.10.2022 00:02:15
1
22
1000
01.10.2022 00:02:30
1
22
500
01.10.2022 00:02:40
1
22
500
01.10.2022 00:02:42
1
22
0
01.10.2022 00:02:45
This is how the plot of the signal looks like.For the above image, measure should calculate the counter as 3.
I wrote a measure in DAX, to increase the count by 1, if the signal value goes above 500.But this did not work since I am sampling the data quite fast which results in reading the same data again(See the column Value for row number 4,5 and 6).
Counter = CALCULATE (COUNTROWS (tableName), FILTER (tableName, tableName[Value]>500))
Another way would be to determine the slope of the signal i.e to find that the Value column has crossed 500 in upward slope and returned to 500 again in the downward slope.Can I calculate slope like this?
I would identify period start with a column as follows:
Period Start =
VAR cursor = tbl[Value]
VAR val = CALCULATE(SUM(tbl[Value]),OFFSET(1,SUMMARIZE(tbl, tbl[DateTime], tbl[Value]),ORDERBY(tbl[DateTime])), ALLEXCEPT(tbl, tbl[DateTime]))
RETURN
if(cursor ==0 && val>0, 1)
I would then have a running total showing which period each row falls into.
Period =
VAR cursor = tbl[DateTime]
RETURN
CALCULATE(SUM(tbl[Period Start]), tbl[DateTime] <= cursor, REMOVEFILTERS() )
If have a huge table then I'd add 2 calculated column. The reason is that some calculations would be done before data manipulating.
col 1 :
id =
RANKX(
CALCULATETABLE(
tbl
,ALLEXCEPT(tbl,tbl[DeviceID],tbl[TesterID])
)
,[DateTime]
,[DateTime]
,ASC
,Skip
)
col2 :
col =
LOOKUPVALUE(
tbl[Value]
,tbl[id]
,[id]-1
,tbl[TesterID]
,[TesterID]
,tbl[DeviceID]
,[DeviceID]
)
+[Value]
The second column will have 2 values of 1500 for each full wave within DeviceID-TesterID.
So, you can try then following trick:
Sumx(
ADDCOLUMNS(
SUMMARIZE(
tbl
,tbl[TesterID]
,tbl[DeviceID]
)
,"q"
,COUNTROWS(
Calculatetable(
tbl
,tbl[col]=1500
)
)
)
,INT([q]/2)
)
As a result it should be a number of full waves... I believe )
I have two below tables in Dim_Date the Period doesn't correspond to exact months and first I calculated a measure to Divide the count of records per region by NumofWeeks.
The Num of the week is always the same in the same period.
Table1
Region
Subregion
DataID
Amount
North Central
Missouri
12042022
123000
North Central
Minnesota
12052022
170000
North Central
North Dakota
10042022
234000
Northeast
New York
08042022
500000
Northeast
New Jersey
12052022
578000
Southwest
Nevada
12032022
679000
Southwest
Arizona
10032022
654000
DimDate
DataID
Period
NumofWeeks
12052022
2022_05
5
10042022
2022_04
4
12042022
2022_04
4
12052022
2022_05
5
08042022
2022_04
4
12032022
2022_03
4
10032022
2022_03
4
Divide per region week =
VAR TotalCountPerRegion = COUNT(Table1[Region])
VAR tblNumOfWeeksInPeriod=
SUMMARIZE(
DimDate
,DimDate[Period]
,DimDate[NumofWeeks]
)
VAR SuMOfWeeksInPeriod = SUMX(tblNumOfWeeksInPeriod,DimDate[NumofWeeks])
RETURN
MROUND(
DIVIDE(
TotalCountPerRegion
,SuMOfWeeksInPeriod
)
,1
)
With the formula, I got the following table
Period
Divide per region week
2021_04
17
2021_05
15
2021_06
9
2021_07
16
2021_08
20
2021_09
21
2021_10
17
2021_11
19
2021_12
20
2022_01
27
I want a new formula to calculate the average of the period with 2 following periods
Example
Average 2021_04 = 2021_04 + 2021_05 + 2021_06 = (17+15+9)/3
Average 2021_05 = 2021_05 + 2021_06 + 2021_07 = (15+9+16)/3
and so on.
thank you in advance.
I accepted this solution thank you very much ...
[M 3-Period Rolling Avg] =
// Set the number of periods to calculate
// the average over.
var PeriodCount = 3
// First, get the last visible period.
var LastPeriodSeqno = MAX( Dim_Date[PeriodSeqno] )
// Then, get the periods over which to calc.
var PeriodsToAverageOver =
CALCULATETABLE(
DISTINCT( Dim_Date[PeriodSeqno] ),
// Here's the place where you use the fact that
// all the periods are consecutively numbered.
// In fact, the counting does not have to start
// at 1 but it has to increment by 1.
Dim_Date[PeriodSeqno] <= LastPeriodSeqno,
Dim_Date[PeriodSeqno] > LastPeriodSeqno - PeriodCount,
REMOVEFILTERS( Dim_Date )
)
// We need to make sure that there are indeed
// PeriodCount periods in the set. Otherwise,
// the average will not be correct. This could happen
// if we were too close to the beginning of the calendar.
var ShouldCalculate =
COUNTROWS( PeriodsToAverageOver ) = PeriodCount
var Result =
if( ShouldCalculate,
CALCULATE(
AVERAGEX(
PeriodsToAverageOver,
[M]
),
REMOVEFILTERS( Dim_Date )
)
)
return
Result
My calendar is different for example the periods are like a financial calendar the period
2021_04 - Starts on 05/04/2021 and finishes on 02/05/2021
2021_05 - Starts on 03/05/2021 and finishes on 30/05/2021
2021_06 - Starts on 31/05/2021 and finishes on 04/07/2021
I want to calculate
Divide per region week =
VAR TotalCountPerRegion = COUNT(Table1[Region])
VAR tblNumOfWeeksInPeriod=
SUMMARIZE(
DimDate
,DimDate[Period]
,DimDate[NumofWeeks]
)
VAR SuMOfWeeksInPeriod = SUMX(tblNumOfWeeksInPeriod,DimDate[NumofWeeks])
RETURN
MROUND(
DIVIDE(
TotalCountPerRegion
,SuMOfWeeksInPeriod
)
,1
)
Rolling 3 periods if we start in 2021_04 I have
2021_06 = Average(2021_06+2021_05+2021_04)
2021_07 = Average(2021_07+2021_06+2021_05)
2021_08 = Average(2021_08+2021_07+2021_06)
and so on
I need to incorporate the formula above because the average is the sum of the formula for 3 periods.
Thanks in advance
I have two tables - Customer and Transaction. The transaction holds invoices and payments. I need to get the date when the transaction sum is 0 or grater.
So the expected result should be John 02-20-2021. How can I achieve this?
customerID
Name
1
John
2
Ben
customerID
value
date
1
-150
02-13-2021
1
100
02-14-2021
1
200
02-20-2021
1
10
02-23-2021
You can try this out, by adding some new measures.
First measure to calculate the account balance over dates, returning a value only for positive balance:
Account Balance :=
VAR _date = MAX ( Transaction[date] )
VAR _balance =
CALCULATE (
SUM ( Transaction[value] ) ,
Transaction[date] <= _date
)
RETURN
IF ( _balance >= 0 , _balance )
The second filters the original table based on this measure and returns the earliest date:
Break Even Date :=
MINX (
FILTER (
VALUES ( Transaction[date] ),
[Account Balance]
),
[date]
)
Result:
Do note that I have not tested this beyond your (simple) example.
So I have a table that has the output of all machines in a department with styles. For example:
|Machine| |Style| | QTY| |Time| |Date| etc...
1 001 100 8:00AM 5/21/19
2 001 200 8:05AM 5/21/19
1 001 100 9:00AM 5/21/19
1 004 100 10:00AM 5/21/19
2 001 200 9:05AM 5/21/19
I'm looking to see the amount of times a style is changed for a machine. So in this case, for Machine 1 it was one style change and for Machine 2 it was zero.
I've tried adapting some code to no avail; mainly because I'm having trouble understanding the logic and I can't really think of a good index to work with.
Here is what I got so far:
EarliestChange Over Index =
VAR Temp =
CALCULATE (
MAX ( Table[Index] ),
FILTER (
Table,
[Index] < EARLIER ( [Index] )
&& [Style] <> EARLIER ( [Style])
&& Table[Date] = today()-1
)
)
VAR Temp1 =
CALCULATE (
MIN ( [Index] ),
FILTER (
Table,
[Index] > EARLIER ( [Index] )
&& [Style] <> EARLIER ( [Style])
&& Table[Date] = today()-1
)
)
RETURN
IF ( [Index] > temp && OR ( [Index] < temp1, ISBLANK ( temp1 ) ), temp + 1, 0 )
I tried to restrict it to just one day so that I could evaluate the results so that portion can be dropped. I've tried two different indexes, one was the machine number and the other was the difference in time from today and the min date on the table. In a visual, I've been taking a distinct count of the EarliestChange Over Index and subtracted one since it didn't constitute a "change over."
EDIT:
Issue where multiple styles are logged at the same time causing false change overs.
|Machine| |Style| | QTY| |Time| |Date| etc...
1 001 100 8:00AM 5/21/19
1 001 100 9:00AM 5/21/19
1 004 100 10:00AM 5/21/19
1 004 100 10:00AM 5/21/19
1 004 100 10:00AM 5/21/19
In one department a time would never be duplicated. However, in another department (for whatever reason) might log 3 rolls at the same time. This would cause the equation to log 10:00am as 3 change overs. It might be a system glitch why there isn't unique time stamps per roll but this is the case unfortunately.
One way of doing it:
First, I modified your data as follows:
Added a record for Machine 1 at 11:00AM to capture a situation when a style reverts to the old one;
Added a column for Date-Time (simply Date + Time), to make life easier;
Named the table as "Data"
Measure:
Style Change Count
=
SUMX (
Data,
VAR Current_DateTime = Data[Date-Time]
VAR Current_Style = Data[Style]
VAR Previous_DateTime =
CALCULATE (
MAX ( Data[Date-Time] ),
FILTER ( ALLEXCEPT ( Data, Data[Machine] ), Data[Date-Time] < Current_DateTime )
)
VAR Previous_Style =
CALCULATE (
VALUES ( Data[Style] ),
FILTER ( ALLEXCEPT ( Data, Data[Machine] ), Data[Date-Time] = Previous_DateTime )
)
RETURN
IF ( Current_Style = Previous_Style || ISBLANK ( Previous_Style ), 0, 1 )
)
Result:
How it works:
We need to use SUMX to make sure that our subtotals and totals are correct;
SUMX iterates over Data table and for each record computes "Previous date-time", which is simply the max datetime less than the current datatime, per machine (hence ALLEXCEPT);
Then, we can calculate Previous Style, which is a style where date-time = previous date-time;
Finally, we compare current style and previous style. If they are not the same, we add 1;
In addition, I added a test for the starting condition - first occurrence of a machine, for which previous style does not exist yet. I did not treat such records as "style change". If you want to count initial records as style change, remove ISBLANK() part.
I have created a measurement (Sales/budget Percentage) that returns the performed output regarding sales/budget. If the percentage output of this measurement is greater or equal to 71% then return 1.
I then need to count the No of occurrences from this measurement that is 1.
I have tried multiple syntax such as Countx, Controws(filter etc. but al of these needs a table and column which I don't provide because it is a measurement.
this is my simple measurement to retrieve a 1 if value is true,
Percentage = sales/budget
Green = IF([Percentage]>=0,701;1;0)
My table is as follows for example
Sales Budget (Measurement) Percentage (Measurement) Green
100 80 125% 1
50 100 50% 0
100 100 100% 1
My Measurement which then would count No of occurrences of 1 or 0 in table above would return as below but this I can't seem to figure out.
No 1 = 2
No 0 = 1
How do I do this since my tests with counting above values = IF Above 70% is 1 doesn't seem to work?
Create a calculated column using the following DAX:
[Above 70% Flag] :=
VAR Result =
CALCULATE ( DIVIDE ( 'Table'[Sales], 'Table'[Budget] ) )
RETURN
SWITCH ( TRUE (), Result >= .71, 1, 0 )
Then, create your measure referencing your new column:
[Count Above 70%] :=
CALCULATE ( COUNT ( [Above 70% Flag] ), [Above 70% Flag] > 0 )
[Count Below 70%] :=
CALCULATE ( COUNT ( [Above 70% Flag] ), [Above 70% Flag] = 0 )