I'm calculating the difference of "closed column". All data is in one column and I'm calculating the difference between Row2-Row1 for all the rows. I'm getting results as some positive values and some negative. Positive values are coming correct but negative values are incorrect. I'm applying the formula
diff =
Table3[Value] -
CALCULATE(
SUM (Table3[Value]),
FILTER(
Table3,
Table3[Index] = EARLIER(Table3[Index])- 1
)
).
Screenshot of my formula
Output after applying formula, -ve and +ve values
Please help how can I correct my -ve values?
Month Week Month End Closed Open GT IN
01/2020 W01-2020 N 71 178 249 71
01/2020 W02-2020 N 284 189 473 213
01/2020 W03-2020 N 550 210 760 266
01/2020 W04-2020 N 861 185 1046 311
01/2020 W05-2020 Y 1185 205 1390 324
02/2020 W06-2020 N 370 206 576 370
02/2020 W07-2020 N 665 209 874 295
In Power Query Editor, I have added an Index column started from 1 to the data and the output is as below-
Now, create this below measure to get previous rows Closed value in the current row-
prev_row_closed_value =
CALCULATE(
SUM (your_table_name[Closed]),
FILTER(
ALL(your_table_name),
your_table_name[Index] = MIN(your_table_name[Index]) - 1
)
)
For calculating difference, use this below measure-
diff =
MIN(your_table_name[Closed]) -
CALCULATE(
SUM (your_table_name[Closed]),
FILTER(
ALL(your_table_name),
your_table_name[Index] = MIN(your_table_name[Index]) - 1
)
)
Here is output from the above measure-
In Dax you can use the following formulas.
In step one we create a column to get your Week Column in an order:
YearWeek = CONVERT(RIGHT(Sheet1[Week], 4) & MID(Sheet1[Week],2,2),INTEGER)
This is creating an integer value our of your year and month. Next we can use this to get the previous closed amount to be substracted where we filter first on the correct month. Be aware that I take the assumption this is a date column.
In =
var curMonth = Sheet1[Month]
var curYW = Sheet1[YearWeek]
var filterMonthYW = FILTER(Sheet1, curMonth = Sheet1[Month] && curYW > Sheet1[YearWeek])
var MaxYW = CALCULATE(MAX(Sheet1[YearWeek]), filterMonthYW)
return Sheet1[Closed] - CALCULATE(MAX(Sheet1[Closed]), FILTER(filterMonthYW, MaxYW = Sheet1[YearWeek] ))
Sheet1 is your table..
End result:
Related
I have two table that Table1 contain item and balance, Table 2 contain item and consume, how to calculate the running balance as new column in table 2? (Balance - consume might be negative: shortage)
Table 1
Item
Balance
A
100
B
200
C
500
Table 2
Item
consume
A
10
A
20
A
20
B
120
B
100
C
100
C
100
C
200
Expected:
Item
consume
Running Balance
A
10
90
A
20
70
A
20
50
B
120
80
B
100
-20
C
100
400
C
100
300
C
200
100
Open your Table 2in the PowerQuery editor and from the Add Column tab select Index Column
Create a measure with the following expression:
running subtract =
VAR cursor =
MAX('Table 2'[Index])
VAR runtot =
CALCULATE(
SUM('Table 2'[consume]),
'Table 2'[Index] <= cursor
)
VAR balance =
LOOKUPVALUE('Table 1'[Balance],'Table 1'[Item], MAX('Table 2'[Item]))
RETURN
balance - runtot
Put everything together in a table visual
I want to achive do cumulate values per day per product and reset the value for every new year.
What I have:
Date
productID
value
01.01.2022
1270
30000
01.01.2022
1280
200000
02.01.2022
1280
-50
01.02.2022
1280
100
01.02.2022
1280
200
01.02.2022
1270
-20
01.03.2022
1270
80
29.12.2022
1270
100
29.12.2022
1280
10
31.12.2022
1270
35
31.12.2022
1270
5
01.01.2023
1270
50000
01.01.2023
1280
100000
04.01.2023
1270
50
06.01.2023
1280
-100
Value should be calculated cumulative per day with a fresh start from each year and per productID.
What I want as a measure is Cumulative Per Year.
Date
productID
value
Cumulative per Year
01.01.2022
1270
30000
30000
01.01.2022
1280
200000
200000
02.01.2022
1280
-50
199950
01.02.2022
1280
100
200050
01.02.2022
1280
200
200250
01.02.2022
1270
-20
29980
01.03.2022
1270
80
30060
29.12.2022
1270
100
30160
29.12.2022
1280
10
200260
31.12.2022
1270
35
30195
31.12.2022
1270
5
30200
01.01.2023
1270
50000
50000
01.01.2023
1280
100000
100000
04.01.2023
1270
50
50050
06.01.2023
1280
-100
99900
What I tried:
Cumulative per Year =
VAR varProductID = SELECTEDVALUE(MyTable[productID])
VAR varYear = SELECTEDVALUE(MyTable[date])
CALCULATE(SUM(MyTable[Value],
FILTER(MyTable,
varProductID = MyTable[productID] &&
varYear = MyTable[date]
)
What I also tried is STARTOFYEAR() and ENDOFYEAR() to know when the cumulative should reset but I not meant to work with selectedvalue() also for some reason MyTable[date].Year wont work.
Thanks for any help.
this should work...
Cumulative per Year =
VAR _date =
SELECTEDVALUE ( 'Table'[Date] )
VAR _id =
SELECTEDVALUE ( 'Table'[productID] )
RETURN
CALCULATE (
SUM ( 'Table'[value] ),
FILTER (
ALL ( 'Table' ),
_id = 'Table'[productID]
&& 'Table'[Date].[Year] = YEAR ( _date )
&& 'Table'[Date] <= _date
)
)
What you could try is TOTALYTD time intelligence measure and drop it in as a last column and see if it works.
It would look something like this:
Cummulative Total=TOTALYTD(SUM(Table[Value]),DateTable[Date])
If you don't have it already you will need to create a date dimension table, see link here for a steps on how to create a Calendar table > https://radacad.com/all-in-one-script-to-create-calendar-table-or-date-dimension-using-dax-in-power-bi
Create a relationship between calendar table date > your table date and then use cummulative total measure. Always best to have date dimension table.
I have 4 Categories (GP, ID, Age, Date). I would would like to create calculated column and group by GP, ID, and Age and Rank/ count by Date to see how many months each member has in past 24 month.
My Code works until I have members who cancelled their membership for a few months and then resumed after. I need to restart from the first month after skip. for example :
GP ID AGE DATE RKING Desired RANK
1 220 35-44 202206 12 6
1 220 35-44 202205 12 5
1 220 35-44 202204 12 4
1 220 35-44 202203 12 3
1 220 35-44 202202 12 2
1 220 35-44 202201 12 1
1 220 35-44 202012 24 24
1 220 35-44 202011 23 23
1 220 35-44 202010 22 22
1 220 35-44 202009 21 21
1 220 35-44 202008 20 20
1 220 35-44 202007 19 19
1 220 35-44 202006 18 18
1 220 35-44 202005 17 17
1 220 35-44 202004 16 16
… … … … … …
1 220 35-44 201901 1 1
This is what I have tried but doesn't work for dates skipping.
RKING Column=
RANKX (
CALCULATETABLE (
VALUES ('tbl'[Date] ),
ALLEXCEPT ( 'tblW', 'tbl'[GP], 'tbl'[ID] ),
'tbl'[AGE] = 'tbl'[AGE],
'tbl'[date] >= start_date && 'tbl'[date] <= end_date // date slicer
),
[Date] ,
,ASC
)
Looking through the code you were trying to make a measure for a visual (For a calcCol the measure is added as well). And as I got a point, you want to show a sum of consequtive months in a matrix for each date in accordance to ID/GP/AGE/DATE I see a following way.
As you know, calculations performs for each row in a matrix and filter the data model according to data presented in matrix rows and columns (slicers as well). So, my idea is -
Get date from matrixRow and use it as max date for the table.
Then use a FILTER(). FILTER() is an iterative function, so it goes throw each row and checks filtering condition - if true row remains if false - not.
I use following filtring conditions:
Get dateInMatrix-dateInACurrentTableRow (for example: 202203-202201= 2 months)
Then check how many rows in the table with min=202201 and max<202203
if there are less rows then date difference then it FALSE() and the row is out of table.
3) The last step is counting of rows it a filtered table.
A measure for matrix:
Ranking =
VAR matrixDate=MAX('table'[DATE])
VAR filteredTable =
FILTER(
ALL('table')
,DATEDIFF(
DATE(LEFT([DATE],4),RIGHT([DATE],2),1)
,DATE(LEFT(matrixDate,4),RIGHT(matrixDate,2),1)
,MONTH
)
=
VAR dateInRow=[DATE]
RETURN
CALCULATE(
COUNTROWS('table')
,'table'[DATE]>=dateInRow
,'table'[DATE]<matrixDate
)
)
RETURN
COUNTROWS(filteredTable)
[![enter image description here][1]][1]
A measure for calcColl:
RankColl =
VAR currentDate=[Start_Date]
Var MyFilt={('Table'[AGE],'Table'[ID],'Table'[GROUP])}
VAR withColl =
ADDCOLUMNS(
CALCULATETABLE(
'table'
,ALL('Table')
,TREATAS(MyFilt,'Table'[AGE],'Table'[ID],'Table'[GROUP])
)
,"dateDiff",
DATEDIFF(
[Start_Date]
,currentDate
,MONTH
)
,"RowsInTable",
VAR dateInRow=[Start_Date]
Var startDate=IF(dateInRow<currentDate,dateInRow,currentDate)
VAR endDay =IF(dateInRow>currentDate,dateInRow,currentDate)
VAR myDates = GENERATESERIES(startDate,endDay,1)
RETURN
COUNTROWS(
CALCULATETABLE(
'Table'
,ALL('Table')
,TREATAS(MyFilt,'Table'[AGE],'Table'[ID],'Table'[GROUP])
,TREATAS(myDates,'Table'[Start_Date])
)
)
)
VAR filtered =
FILTER(
withColl
,[dateDiff]=[RowsInTable]-1 -- for ex.:
-- dateDiff=01/01/2022-01/01/2022=0,
-- but it will be 1 row in the table for 01/01/2022
)
RETURN
CountRows( filtered)
I have two tables, with:
Entrydate, several categories
ChurnDate, several categories
The categories are connected via different tables, and the dates are connected with a Calendar.
Now I want to calculate how many customers I have. So I have following DAX formulas
1. SumChurn =
CALCULATE(
SUM('kuendigungen'[KUENDIGUNG]);
FILTER(
ALLSELECTED('Calendar'[Date]);
ISONORAFTER('Calendar'[Date]; MAX('Calendar'[Date]); DESC)
)
)
2. SumEntry =
CALCULATE(
SUM('eintritt'[NEUMITGLIED]);
FILTER(
ALLSELECTED('Calendar'[Date]);
ISONORAFTER('Calendar'[Date]; MAX('Calendar'[Date]); DESC)
)
)
3. TotalCustomers = SumEntry - SumChurn
This works, but in my diagram I want to filter the dates, so that it only visualizes 2020 or the last 3 years.When I do this the calculation is wrong because it only counts in this interval.
Is there a solution that I can filter the date in my visuals but in my calculation the start date of the cummulative sum is always fixed?
I dont't want a new column because I still want to filter my categories of customers...
Thanks,
Michaela
Edit: Try to explain it clearer
Example Table 1: contains new customers
Date unique_id1 unique_id2 unique_id3 cat1 cat2 cat3 cat4 cat5 cat6
1886-02-01 2070030124 550261 207000152145 207 0 0 1 0 0
1887-01-01 4350002756 4081878 435000010707 435 0 0 1 0 0
1888-01-01 7030000597 3206858 703000001279 703 0 0 1 0 0
1888-06-01 7030016696 3208056 703000005002 703 0 0 1 0 0
1888-09-01 8210024182 204124 821000008664 821 1 0 1 0 1
1889-01-01 7050055324 1988250 705000018309 705 1 0 1 0 0
1889-01-01 8250000278 439485 825000600296 825 0 0 1 0 0
1889-05-01 7030023754 3208355 703000000884 703 0 0 1 0 0
1889-10-01 2110071206 2849359 211000330019 211 0 1 1 0 0
1889-10-01 2110071236 2851371 211000120014 211 0 0 1 0 0
1889-11-14 5190529889 4260192 519000123846 519 1 0 1 0 0
1890-07-01 7330349030 4819467 733000013102 733 0 0 1 0 0
1890-07-01 7330152914 4817492 733000075604 733 1 0 1 0 1
1890-07-01 8190000889 486170 819000215708 819 0 0 1 0 0
1890-07-01 8190444976 486199 819000215740 819 0 0 1 0 0
1890-12-01 8190001388 476049 819000100005 819 0 0 1 0 0
1891-01-01 7030001248 3206975 703000000043 703 0 0 1 0 1
Example Table 2: contains leaving customers
similiar to table 1
Example Calendar Table:
01.01.1990
02.01.1990
03.01.1990 ... (till today)
Output shut be a measure
for each day in calendar: number of customer at this date = cumulative_sum(newcustomer) - cumulative_sum(churncustomer)
I get exactly this output, when I run the calculations I wrote, but I want the measure in a way, ehen I filter the date, the sum is still the cummulative sum from the very first date, otherwise the numbers are wrong.
Edit3:
I did exactly the same thing, as mkrabbani posted, but it doesnt't work for me, following calculations:
TotalKuendigungen =
CALCULATE(
SUM('kuendigungen'[KUENDIGUNG]);
FILTER (
ALL ( 'Calendar'[Date] );
( 'Calendar'[Date] <= MAX ( ( 'Calendar'[Date] ))
)))
TotalNeukunden = CALCULATE(
SUM('eintritt'[NEUMITGLIED]);
FILTER (
ALL ( 'Calendar'[Date] );
( 'Calendar'[Date] <= MAX ( ( 'Calendar'[Date] ))
)))
AnzahlMitglieder = [SummeNeumitglied] - [SummeKuendigung]
This is how it looks for me: (Neukunden: new customers, kündigungen: leaving, aktuellemitglieder: number of customers)
Picture 1 correct calculation
Picture 2: also correct calculation, but filter doesnt work
thanks for adding some sample data with more explanation. If I get your requirement correct, this below steps with explanation will help you solving your issue I hope.
Assumption: If my understanding is correct, you have 3 tables with Date, new_customer and leaving_customer and they are related as below diagram shown.
Now, I have created some sample data for 10 days, to visualize your requirement/issue. Hope, cumulative counts in the below table is correctly calculated (using basics of cumulative calculation).
At this stage, you need a measure that will calculate current number of customer for each row based on calculation > "cumulative_new_customer - cumulative_leaving_customer" which is not a tough job for you.
But, you are having issue when you are slicing your data using Date slicer. If you are selecting date number 5, which is "January 05 2020" in my sample data. You wants the final counts based on date January 01 to 05, but you are getting only counts from one single date "January 05 2020".
If the above explanation is correct, I would suggest to write 3 separate Measure as explained below in this answer. You can have a look on the output in the below picture I have added with comparison with before and after slicing the data. You can see the number of current user for "January 05 2020" is 41 for both case (Before and After Slicing)
Now, if everything above is meeting your expectation, you can use this below 3 measures as written.
1.
cumulative_new_customer =
CALCULATE (
COUNT(new_customer[unique_id]),
FILTER (
ALL ( 'Dates'[Date] ),
'Dates'[Date] <= MAX ( 'Dates'[Date] )
)
)
2.
cumulative_leaving_customer =
CALCULATE (
COUNT(leaving_customer[unique_id]),
FILTER (
ALL ( 'Dates'[Date] ),
'Dates'[Date] <= MAX ( 'Dates'[Date] )
)
)
3.
number_of_cutomer_today = [cumulative_new_customer] - [cumulative_leaving_customer]
Hope the above details will help you.
I am struggling to get this going and could need some help. I have the following setup:
Order Item Material Value
22 1 100 27,5
22 1 200 27,5
22 1 300 27,5
22 2 100 33
22 3 500 101
26 1 500 88
26 1 600 88
I have duplicate values becaue of the Material, so I want to group by Order, Item and Value and then calculate the total Value in a DAX measure.
After grouping:
Order Item Value
22 1 27,5
22 2 33
22 3 101
26 1 88
The final Value:
Total Measure = 249,5
I tried the following DAX expression for the Total Measure:
Total Measure = Summarize('Table1'; 'Table1'[Order]; 'Table1'[Item]; "Sum Value:"; Sum('Table1'[Value]))
It gives me the error:
Multiple columns cannot be converted to a scalar value
So I tried:
Total Measure = Sumx('Table1'; Summarize('Table1'; 'Table1'[Order]; 'Table1'[Item]; "Sum Value:"; Sum('Table1'[Value])))
But this didnt work either. For every help thanks in advance.
The following code should be what you are looking for
Measure1 =
SUMX (
SUMMARIZE (
Table1;
Table1[Order];
Table1[Item];
Table1[Value];
"TotalSum"; SUM ( Table1[Value] )
);
[Value]
)
In this case, you can simply use the VALUES function instead of SUMMARIZE.
Total Measure = SUMX ( VALUES ( Table1[Value] ), [Value] )
This iterates over each unique Value and adds Value to the sum.