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
Related
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 want to numerate the occurances of a specific column in my table. The best way I have thought to do this is to count the rows of a filtered table. So, my WorkOrders table looks like this:
WO# Date CompCode Serial#
001 1/1/2021 100 A
001 1/1/2021 101 A
002 1/2/2021 100 B
003 2/1/2021 100 A
004 2/2/2021 100 B
005 2/15/2021 101 A
006 3/1/2021 102 A
006 3/1/2021 100 A
I want to create a new column that numerates the occurance on the CompCode by Serial#. There is no gaurantee that the data is sorted by date. So, I tried to count the rows of a filtered table using this formula:
COMP_OCCURANCE =
CALCULATE(
COUNTROWS(WorkOrders),
Serial# = Serial#,
Date <= Date,
CompCode = CompCode
)
I assumed that would work but it does not. The desired result would look like this:
WO# Date CompCode Serial# COMP_OCCURANCE
001 1/1/2021 100 A 1
001 1/1/2021 101 A 1
002 1/2/2021 100 B 1
003 2/1/2021 100 A 2
004 2/2/2021 100 B 2
005 2/15/2021 101 A 2
006 3/1/2021 102 A 1
006 3/1/2021 100 A 3
Thanks in advance for the help.
Try this:
COMP_OCCURANCE =
VAR CurrentSerial = WorkOrders[Serial #]
VAR CurrentDate = WorkOrders[Date]
VAR CurrentCode = WorkOrders[CompCode]
VAR Result =
CALCULATE (
COUNTROWS ( WorkOrders ),
WorkOrders[Serial #] = CurrentSerial,
WorkOrders[CompCode] = CurrentCode,
WorkOrders[Date] <= CurrentDate,
REMOVEFILTERS ( WorkOrders )
)
RETURN
Result
Screenshot - https://ibb.co/Bj5xrFS
For this type of calculation, you must need to rely on a column for sorting/ordering your data. As you cannot use Date in this case, I think you can go for column "WO#" this case. If this is acceptable, you can try this below measure for your expected output-
COMP_OCCURANCE =
CALCULATE(
COUNT(your_table_name[WO#]),
FILTER(
ALL(your_table_name),
your_table_name[WO#] <= MIN(your_table_name[WO#])
&& your_table_name[CompCode] = MIN(your_table_name[CompCode])
&& your_table_name[Serial#] = MIN(your_table_name[Serial#])
)
)
Output-
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:
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.