Running Sum # and Running Sum % based on status Power BI (DAX) - powerbi

I am struggling with creating Running Summ for value based on two statuses I have in my table. The problem is that I do not have dates, only text and numeric values.
I even created Index table but this does not help. Please have a look at my data:
I need to calculate Running Sum for Column Distribution in another Column but for status "Gains" and "Gross" separately. So the Running Sum is calculated for "Gains" and then starts again for "Gross".
Then I need to use that to create Percent of Total - also separately for "Gains" only and for "Gross" only. I reviewed many forums, tutorials and could not find anything working for my data.
Can you please help me out?
Data sample:
Score Range
tier
Distribution
Status
General Index
1-100
Tier III
38
Gains
1
100-125
Tier III
33
Gains
2
125-150
Tier III
49
Gains
3
150-175
Tier III
46
Gains
4
175-200
Tier III
76
Gains
5
200-225
Tier II
135
Gains
6
225-250
Tier I
348
Gains
7
250-275
Tier I
417
Gains
8
275-300
Tier I
541
Gains
9
300-325
Tier I
682
Gains
10
325-350
Tier I
910
Gains
11
350-375
Tier I
781
Gains
12
375-400
Tier I
754
Gains
13
400-425
Tier I
551
Gains
14
425-450
Tier I
396
Gains
15
450-475
Tier I
214
Gains
16
475-500
Tier I
50
Gains
17
500 +
Tier I
2
Gains
18
No Score
Tier I
176
Gains
19
1-100
Tier III
350
Gross
1
100-125
Tier III
270
Gross
2
125-150
Tier III
404
Gross
3
150-175
Tier III
463
Gross
4
175-200
Tier III
465
Gross
5
200-225
Tier II
512
Gross
6
225-250
Tier I
599
Gross
7
250-275
Tier I
700
Gross
8
275-300
Tier I
897
Gross
9
300-325
Tier I
1089
Gross
10
325-350
Tier I
1415
Gross
11
350-375
Tier I
1183
Gross
12
375-400
Tier I
1104
Gross
13
400-425
Tier I
725
Gross
14
425-450
Tier I
535
Gross
15
450-475
Tier I
282
Gross
16
475-500
Tier I
67
Gross
17
500 +
Tier I
2
Gross
18
No Score
Tier I
624
Gross
19
I am trying to make calculations as on below screen:
Thanks,

I shortened the names of the columns a bit to make the result table stay in the answer
I named the sample data table "Status"
For the Running Sum we iterate filtering the Status of the current row and an index less than or equal to the current row's
Running Sum =
VAR CurrentRowStatus = Scores[Status]
VAR CurrentIndex = Scores[General Index]
VAR Result =
SUMX(
FILTER(
Scores,
Scores[Status] = CurrentRowStatus
&& Scores[General Index] <= CurrentIndex
),
Scores[Distribution]
)
RETURN
Result
For the percentages calculated columns we need to compute the total, therefore we use MAXX over the Status table filtered using the current row status
percent =
VAR CurrentRowStatus = Scores[Status]
VAR Total =
MAXX(
FILTER( Scores, Scores[Status] = CurrentRowStatus ),
Scores[Running Sum]
)
VAR Result =
DIVIDE( Scores[Distribution], Total )
RETURN
Result
the cumulative percent calculated column is similar, it just uses the Running Sum calculated column instead of the Distribution
cumulative percent =
VAR CurrentRowStatus = Scores[Status]
VAR Total =
MAXX(
FILTER( Scores, Scores[Status] = CurrentRowStatus ),
Scores[Running Sum]
)
VAR Result =
DIVIDE( Scores[Running Sum], Total )
RETURN
Result
This is the resulting table
Score Range
tier
Distribution
Status
General Index
Running Sum
percent
cumulative percent
1-100
Tier III
38
Gains
1
38
0.6%
0.6%
100-125
Tier III
33
Gains
2
71
0.5%
1.1%
125-150
Tier III
49
Gains
3
120
0.8%
1.9%
150-175
Tier III
46
Gains
4
166
0.7%
2.7%
175-200
Tier III
76
Gains
5
242
1.2%
3.9%
200-225
Tier II
135
Gains
6
377
2.2%
6.1%
225-250
Tier I
348
Gains
7
725
5.6%
11.7%
250-275
Tier I
417
Gains
8
1142
6.7%
18.4%
275-300
Tier I
541
Gains
9
1683
8.7%
27.1%
300-325
Tier I
682
Gains
10
2365
11.0%
38.2%
325-350
Tier I
910
Gains
11
3275
14.7%
52.8%
350-375
Tier I
781
Gains
12
4056
12.6%
65.4%
375-400
Tier I
754
Gains
13
4810
12.2%
77.6%
400-425
Tier I
551
Gains
14
5361
8.9%
86.5%
425-450
Tier I
396
Gains
15
5757
6.4%
92.9%
450-475
Tier I
214
Gains
16
5971
3.5%
96.3%
475-500
Tier I
50
Gains
17
6021
0.8%
97.1%
500 +
Tier I
2
Gains
18
6023
0.0%
97.2%
No Score
Tier I
176
Gains
19
6199
2.8%
100.0%
1-100
Tier III
350
Gross
1
350
3.0%
3.0%
100-125
Tier III
270
Gross
2
620
2.3%
5.3%
125-150
Tier III
404
Gross
3
1024
3.5%
8.8%
150-175
Tier III
463
Gross
4
1487
4.0%
12.7%
175-200
Tier III
465
Gross
5
1952
4.0%
16.7%
200-225
Tier II
512
Gross
6
2464
4.4%
21.1%
225-250
Tier I
599
Gross
7
3063
5.1%
26.2%
250-275
Tier I
700
Gross
8
3763
6.0%
32.2%
275-300
Tier I
897
Gross
9
4660
7.7%
39.9%
300-325
Tier I
1089
Gross
10
5749
9.3%
49.2%
325-350
Tier I
1415
Gross
11
7164
12.1%
61.3%
350-375
Tier I
1183
Gross
12
8347
10.1%
71.4%
375-400
Tier I
1104
Gross
13
9451
9.4%
80.9%
400-425
Tier I
725
Gross
14
10176
6.2%
87.1%
425-450
Tier I
535
Gross
15
10711
4.6%
91.7%
450-475
Tier I
282
Gross
16
10993
2.4%
94.1%
475-500
Tier I
67
Gross
17
11060
0.6%
94.6%
500 +
Tier I
2
Gross
18
11062
0.0%
94.7%
No Score
Tier I
624
Gross
19
11686
5.3%
100.0%

Related

Dividing two tables in PowerBi

I have two tables, say table 1 and table 2.
Table 1:
Region2
Apr
May
North
50
1200
South
75
1500
East
100
750
West
150
220
Table 2:
Region2
Apr
May
North
5
12
South
10
15
East
10
15
West
15
11
I need a table 3 that is a division of table 1 and table 2
Table 3:
Region2
Apr
May
North
10
100
South
7.5
100
East
10
50
West
15
20
I managed to solve it by creating a measure which is a ratio.
In my case, table 1 had values in say rupees.
table 2 had values in say, litres
So, I created a measure which is a division of sum total rupees and sum total of litres and when I added the measure to values in pivot matrix, it automatically divided the two tables.

REGEX all invoice item descriptions

I'm trying to regex all items from an invoice (name, unit price, total, VAT, etc.). Managed to get all the information regarding digits, but biggest problem si to extract the item descriptions as sometimes it's on two separate lines. This is what I need to regex
1 Agrafe metalice Eco, rotunjite, 33 mm, 50 buc/cutie buc. 30.00 0,76 22,80 4,33
(SOBO604)
2 Banda corectoare DONAU Mouse, 5 mm x 8 m, orizontala, buc. 5.00 4,83 24,15 4,59
blister (7635001PL-99)
3 Biblioraft plastifiat OFFICE Products, 5 cm, colturi buc. 75.00 5,08 381,00 72,39
metalice, albastru (21011121-01)
4 Burete magnetic DONAU, 110 x 57 x 25 mm, galben buc. 10.00 5,53 55,30 10,51
(7638001PL-99)
5 Calculator de birou Canon WS-1610T, solar, 16 cifre, buc. 1.00 71,11 71,11 13,51
afisaz inclinat, format mare (WS1610T)
6 Capse zincate OFFICE Products 24/6, 1000 buc/cutie buc. 5.00 1,12 5,60 1,06
(18072419-19)
7 Creion grafic Eco, ascutit, cu radiera, corp verde buc. 20.00 0,40 8,00 1,52
(SOIS432)
8 Creion mecanic BIC Matic, 0.7 mm (601021) buc. 4.00 1,88 7,52 1,43
9 Dosar din plastic cu sina si doua perforatii OFFICE buc. 250.00 0,35 87,50 16,63
Products, albastru (21104211-01)
10 Dosar din plastic cu sina si doua perforatii OFFICE buc. 100.00 0,35 35,00 6,65
Products, roz (21104211-13)
pagina 1 / 3
797638
11 Folie protectie OFFICE Products, A4, coaja portocala, 40 buc. 5.00 6,53 32,65 6,20
microni, 100 file/set (21141215-90)
12 Folie protectie OFFICE Products, A4, coaja portocala, 40 buc. 20.00 6,51 130,20 24,74
microni, 100 file/set (21141215-90)
13 Marker whiteboard Eco, varf rotund, albastru (SOIS535A) buc. 104.00 1,33 138,32 26,28
14 Marker whiteboard Eco, varf rotund, negru (SOIS535N) buc. 2.00 1,33 2,66 0,51
15 Marker whiteboard Eco, varf rotund, rosu (SOIS535R) buc. 2.00 1,33 2,66 0,51
16 Notite adezive OFFICE Products, 51 x 76 mm, galben pal, buc. 5.00 1,65 8,25 1,57
100 file (14047511-06)
17 Organizator de birou DONAU Clasic VII, 6 compartimente, buc. 2.00 30,67 61,34 11,65
155 x 105 x 101 mm, transparent (7476001-99)
18 Panou din pluta Bi-Office, 60 x 90 cm, rama lemn buc. 1.00 32,96 32,96 6,26
(GMC070012010)
19 Pioneze color Eco, tinte pentru pluta , 40 buc/cutie buc. 1.00 2,16 2,16 0,41
(SOBO612)
20 Pix fara mecanism Eco, varf de 1 mm, albastru (SOIS405A) buc. 110.00 0,33 36,30 6,90
21 Plic C4 (229 x 324 mm), alb, siliconic, 10/set buc. 2.00 2,15 4,30 0,82
(15223619-14)
22 Tus pentru stampila Pelikan, cu picurator, 28 ml, negru buc. 1.00 6,93 6,93 1,32
(351197)
Notice that the item description sometimes is after the total price. Problem is that the space between items isn't even, it's variable, like for e.g. position 8 and 9 are almost linked, compared to position 20 and 21 which have a lot of space between them.
Somebody helped me and got only the first line using
\d{1,2}(.*)(\d+\.\d+\s+)(\d+\,\d+\s{0,1}){3}
this is where I got stuck because of the uneven syntax.
It only gets the first line. For e.g.:
'''
16 Notite adezive OFFICE Products, 51 x 76 mm, galben pal, buc. 5.00 1,65 8,25 1,57
100 file (14047511-06)
'''
it gest only 16 Notite adezive OFFICE Products, 51 x 76 mm, galben pal, buc. 5.00 1,65 8,25 1,57 but not 100 file (14047511-06). The complete invoice description is Notite adezive OFFICE Products, 51 x 76 mm, galben pal, 100 file (14047511-06) when transformed from pdf to text this is how I get the files.
Will need to extract also the last part and merge the first one to get the full item description.
Thank you
Try this regex:
\d{1,2}(.*)(\d+\.\d+\s+)(\d+\,\d+\s?){3}([\n ]+[^(\n]*\([^)]+\)(?=\n))?
Test on regex101

My question is about calculating an indicator based on column total using DAX

I have a table given below. I want to create an indicator which will be based on the column total using DAX. E.g Company A with YoY 13% would have an indicator value 1 as it is more than equal to YoY column total of 8%. I want the similar indicator for all the companies. It should automatically change based on filter/slicer values in Power BI
Company Pax 2019 YoY(%)
A 87 13%
B 45 9%
C 57 9%
D 82 2%
E 53 4%
F 57 8%
G 84 12%
Grand Total 465 8%
I tried it using all table but it changes as the filter changes the value.
Company Pax 2019 YoY(%) Indicator(if grand total YoY> individual YoY, 1, 0)
A 87 13% 1
B 45 9% 1
C 57 9% 1
D 82 2% 0
E 53 4% 0
F 57 8% 0
G 84 12% 1
Grand Total 465 8%
Below DAX Expression should work for you:
Column =
VAR Tot_Average =
AVERAGE ( 'Example'[Pax] )
VAR Check =
CALCULATE (
Tot_Average,
ALL ( 'Example'[Pax] )
)
RETURN
IF ( 'Example'[Pax] > Check, 1, 0 )

Add a dynamic constant line based on column in powerbi

I am trying to plot a line chart in powerBI with a reference line based on another columns value. I have data that represents the journeys of different cars on different sections of road. I am plotting those journeys that travel over the same section of road. e.g. RoadId 10001.
Distance JourneyNum Speed ThresholdSpeed RoadId
1 10 50 60 10001
2 10 51 60 10001
3 10 52 60 10001
1 11 45 60 10001
2 11 46 60 10001
3 11 47 60 10001
7 12 20 30 10009
8 12 21 30 10009
9 12 22 30 10009
10 12 23 30 10009
So currently I have:
Distance on x-axis (Axis),
Speed on y-axis (Values),
JourneyNum as the Legend (Legend),
filter to roadId 10001
I want to also add the thresholdSpeed as a reference line or just as another line would do. Any help?
I don't think it's possible (yet) to pass a measure to a constant line, so you'll need a different approach.
One possibility is to reshape your data so that ThresholdSpeed appears as part of your Legend. You can do this in DAX like so:
Table2 =
VAR NewRows = SELECTCOLUMNS(Table1,
"Distance", Table1[Distance],
"JourneyNum", "Threshold",
"Speed", Table1[ThresholdSpeed],
"ThresholdSpeed", Table1[ThresholdSpeed],
"RoadId", Table1[RoadId])
RETURN UNION(Table1, DISTINCT(NewRows))
Which results in a table like this:
Distance JourneyNum Speed ThresholdSpeed RoadId
1 10 50 60 10001
2 10 51 60 10001
3 10 52 60 10001
1 11 45 60 10001
2 11 46 60 10001
3 11 47 60 10001
1 Threshold 60 60 10001
2 Threshold 60 60 10001
3 Threshold 60 60 10001
7 12 20 30 10009
8 12 21 30 10009
9 12 22 30 10009
10 12 23 30 10009
7 Threshold 30 30 10009
8 Threshold 30 30 10009
9 Threshold 30 30 10009
10 Threshold 30 30 10009
Then you make a line chart on this table instead:
Note: It's probably preferable to do this transformation in the query editor though so you don't have redundant tables.

SAS-Create Dynamic Tables Based On ID With Only Top 3 Showing

Hi I would really like to create dynamic tables based on the following sample data, create 4 new data sets based upon PAYEE_ID: 522,622,743,and 888. I want all all of the fields to be in the new 4 data sets, but only have the top 3 AMT_BILLED in the 4 tables for each type of PAYEE_ID
PAYEE_ID PAYEENAME MSG_CODE MSG_DESCRIPTION AMT_BILLED percentbilled claimscounts PercentLines TotalAmount TotNumofClaims
522 MakeBelieve Center 1 AA text field 1 10000 4% 50 16% 275000 305
522 MakeBelieve Center 1 BB text field 2 20000 7% 40 13% 275000 305
522 MakeBelieve Center 1 6N text field 3 30000 11% 30 10% 275000 305
522 MakeBelieve Center 1 5U text field 4 25000 9% 20 7% 275000 305
522 MakeBelieve Center 1 1F text field 5 90000 33% 100 33% 275000 305
522 MakeBelieve Center 1 2E text field 6 100000 36% 65 21% 275000 305
622 Invisible Center 2 A4 text field 1 600 2% 9 7% 34300 134
622 Invisible Center 2 D2 text field 2 700 2% 31 23% 34300 134
622 Invisible Center 2 D4 text field 3 8000 23% 11 8% 34300 134
622 Invisible Center 2 DS text field 4 10000 29% 62 46% 34300 134
622 Invisible Center 2 F8 text field 5 15000 44% 21 16% 34300 134
743 Pretend Center 1 1K text field 1 440 1% 2 1% 41040 246
743 Pretend Center 1 1N text field 2 3000 7% 7 3% 41040 246
743 Pretend Center 1 1V text field 3 400 1% 4 2% 41040 246
743 Pretend Center 1 2W text field 4 15000 37% 63 26% 41040 246
743 Pretend Center 1 3B text field 5 500 1% 2 1% 41040 246
743 Pretend Center 1 3H text field 6 7700 19% 41 17% 41040 246
743 Pretend Center 1 3Z text field 7 14000 34% 127 52% 41040 246
888 It's A MakeBelieve One B7 text field 1 68000 38% 257 29% 178449 886
888 It's A MakeBelieve One B8 text field 2 5000 3% 47 5% 178449 886
888 It's A MakeBelieve One B9 text field 3 200 0% 138 16% 178449 886
888 It's A MakeBelieve One BB text field 4 1562 1% 18 2% 178449 886
888 It's A MakeBelieve One BO text field 5 39999 22% 3 0% 178449 886
888 It's A MakeBelieve One BZ text field 6 40000 22% 2 0% 178449 886
888 It's A MakeBelieve One C2 text field 7 500 0% 5 1% 178449 886
888 It's A MakeBelieve One C5 text field 8 7865 4% 395 45% 178449 886
888 It's A MakeBelieve One C7 text field 9 8649 5% 14 2% 178449 886
888 It's A MakeBelieve One CR text field 10 5674 3% 1 0% 178449 886
888 It's A MakeBelieve One CX text field 11 1000 1% 6 1% 178449 886
to
I'm new to SAS, and this would really help me out. Thank you so much!
proc sort data=sampleData out=sampleData_s;
by payee_id amt_billed;
run;
You can use descending if by 'top' you mean largest e.g. by payee_id descending amt_billed;
Once the data are sorted you are able to read into a data step and use first and last e.g.
data partial_solution(drop=count);
retain count 0;
set sampleData_s;
by payee_id descending amt_billed;
if first.payee_id then count=0;
count+1;
if count le 3 then output;
run;
To output to different dataset names:
proc sort data=sampleData(keep=payee_id) out=all_payee_ids nodupkey;
by payee_id;
run;
data _null_;
length id_list $10000; * needs to be long enough to contain all ids;
* if you do not state this, sas will default;
* length to first value;
retain id_list;
set all_payee_ids end=eof;
id_list = catx('|', id_list, payee_id);
if eof then call symputx('macroVarIdList', id_list);
run;
You've now got a pipe separated list of all your id's. You can loop through these using them to create names for you datasets. You need to do this as SAS needs to know the names of the datasets you want to output to up front e.g.
data ds1 ds2 ds3 ds4;
set some_guff;
if blah then output ds1;
else if blahblah then output ds2;
else output d3;
output d4;
run;
So with the macro var loop:
%let nrVars=%sysfunc(countw(&macroVarIdList));
data
%do i = 1 %to &nrVars;
dataset_%scan(&macroVarIdList,&i,|)
%end;
;
set partial_solution;
count+1;
%do j = 1 %to &nrVars;
%let thisPayeeId=%scan(&macroVarIdList,&j,|);
if payee_id = "&thisPayeeId" then output dataset_&thisPayeeId.;
%end;
run;