Power Pivot / Power BI Handling Blanks in Power Pivot/PowerBI - pivot

I have a table, which is as under:
TABLE1
5670 Paid for A 1000 A PK1
5670 Paid for B 200 B PK2
5120 Paid for C 300
5120 Paid for D 400 PK2
5120 Paid for E 500 E PK2
5120 Paid for F 600 F
TABLE2
T1CODE Name
A Group A
B Group B
E Group E
F Group F
The output from the above data:
Row Labels Sum of Amount
(blank) 700
A 1000
B 200
E 500
F 600
Grand Total 3000
Required output using DAX:
Row Labels Sum of Amount
A 1000
B 200
E 500
F 600
Grand Total 3000

Try this:
Sum Of Values =
CALCULATE (
SUM ( 'TABLE1'[Amount] ),
FILTER ( 'TABLE1', 'TABLE1'[Row Labels] <> BLANK () )
)

Related

Mean imputation based on certain Conditions

I have the below dataframe,
Category Value
A 100
A -
B -
C 50
D 200
D 400
D -
As you can see, there are some values which have the hyphen symbol '-'. I want to replace those hyphons with the means of the corresponding category.
In the example, there are two entries for "A" - One row with value 100 and other with hyphen. So the mean would be 100 itself. For B, since there are no valid values, the mean would be the mean of the entire column which would be (100+50+200+400/4 = 187.5). For C, no changes and for D, the hyphen will be replaced by 300 (same logic as for "A").
Output:
Category Value
A 100
A 100
B 187.5
C 50
D 200
D 400
D 300
Try:
df = df.replace("-", np.nan)
df["Value"] = pd.to_numeric(df["Value"])
avg = df["Value"].mean()
df["Value"] = df["Value"].fillna(
df.groupby("Category")["Value"].transform(
lambda x: avg if x.isna().all() else x.mean()
)
)
print(df)
Prints:
Category Value
0 A 100.0
1 A 100.0
2 B 187.5
3 C 50.0
4 D 200.0
5 D 400.0
6 D 300.0

How to sum with Null Values in group by statement using agg function in python

I have a dataframe which looks like:
A B C
a 100 200
a NA 100
a 200 NA
a 100 100
b 200 200
b 100 200
b 200 100
b 200 100
I use the aggregate function on column B and column C as:
ag=data.groupby(['A']).agg({'B':'sum','C':'sum'}).reset_index()
Output:
A B C
a NULL NULL
b 700 600
Expected Output:
A B C
a 400 400
b 700 600
How can I modify my aggregate function so that NULL values are ignored?
Maybe you already though about this but is not possible in your problem, but you can replace the NA values by 0 in the dataframe before this operation. If you donĀ“t want to change the original dataframe you can transform it in a copy.
ag=data.replace(np.nan,0).groupby(['A']).agg({'B':'sum','C':'sum'}).reset_index()

Change array of SUMIF in case criteria exists in two different columns

A B C D E F
1 Results List A List B
2 Campaign Sales Campaign Sales Campaign Sales
3 Campaign_A 1.510 Campaign_A 500 Campaign_B 50
4 Campaign_B 120 Campaign_A 450 Campaign_B 40
5 Campaign_C 90 Campaign_A 560 Campaign_B 30
6 Campaign_D 1.650 Campaign_B 700 Campaign_C 80
7 Campaign_E 100 Campaign_B 710 Campaign_C 10
8 Campaing_F 70 Campaign_C 200 Campaign_F 70
9 Campaing_D 850
10 Campaing_D 800
11 Campaing_E 100
12 Campaing_F 320
13 Campaing_F 360
14 Campaing_F 290
15
16
The Excel table above consists of:
List A = Column C:D
List B = Column E:F
In each list campaigns can appear mutliple times.
In Column A:B I want to sum up the sales per campaign from the two lists using the SUMIF formula:
=SUMIF(C:C,A3,D:D)
=SUMIF(E:E,A3,F:F)
However, the List B should be prioritized over List A which means in case a campaign exists in List B (Column E) the SUMIF function should be only applied to List B and List A should be totally ignored.
The formula might look something like htis:
IF campaign exists in Column E then SUMIF(E:E,A3,F:F) else SUMIF(C:C,A3,D:D)
How can I achieve the desired results in Column B?
Or,
=IF(COUNTIF(E:E,A3)>0,SUMIF(E:E,A3,F:F),SUMIF(C:C,A3,D:D))
I would try with the following:
if(sumIf(E:E,A3,F:F)>0;sumIf(E:E,A3,F:F);sumIf(C:C,A3,D:D))

Array formula to solve distance matrix

Help me with the following problem without adding any helper cells and changing the data:
"There are 8 cities in the country of Eight, A, B, C, D, E, F, G and H. Mr. Z decides to visit each city in his car starting from A. His planned itinerary is A-->B-->C-->D-->E-->F-->G-->H.
The distance between the cities is given in Table I."
Table I
Distance
A B C D E F G H
A 0 200
B 200 0 350
C 350 0 500
D 500 0 250
E 250 0 850
F 850 0 1250
G 1250 0 150
H 150 0
"Write a formula which takes the number of kms that Mr. Z has traveled from A as the input and displays the name of the city which is nearest to that point.
Mr. Z will enter the number of kms he has travelled from A in cell D30 and the name of the city nearest to the point will be displayed in E30"
Presumably a variation on the following (doesn't say can't hardcode cumulative sums)
=INDEX({"A","B","C","D","E","F","G","H"},MATCH(MIN(ABS({0,200,550,1050,1300,2150,3400,3350}-D30)),ABS({0,200,550,1050,1300,2150,3400,3350}-D30),0))

SUMIFS across multiple sheets

So I'm working on a workbook right now that is across 3 sheets, 'Data', 'RAW', 'Lookup'. On the Data tab it looks like, Sheet 1
A B C D E
Segment Ranking Amount Revenue Spend
Carbon 15 2 5000 550
Oxygen 30 3 6895 450
Minerals 45 1 4400 350
The Code I use for each column are as follows (Some of the Columns are named columns)
B =IF(NOT(ISERROR(SEARCH("OK",$A2))),SUMIFS(RAW!B:B,Output,"*"&INDEX(SegmentID,MATCH($A2,SegmentName,0))&"*"),SUMIFS(RAW!B:B,Output,"*"&INDEX(SegmentID,MATCH($A2,SegmentName,0))&"*",Output,"<>*OK*"))
C =IF(NOT(ISERROR(SEARCH("OK",$A2))),SUMIFS(RAW!C:C,Output,"*"&INDEX(SegmentID,MATCH($A2,SegmentName,0))&"*"),SUMIFS(RAW!C:C,Output,"*"&INDEX(SegmentID,MATCH($A2,SegmentName,0))&"*",SegmentName,"<>*OK*"))
D =IF(NOT(ISERROR(SEARCH("OK",$A2))),SUMIFS(RAW!D:D,Output,"*"&INDEX(SegmentID,MATCH($A2,SegmentName,0))&"*"),SUMIFS(RAW!D:D,Output,"*"&INDEX(SegmentID,MATCH($A2,SegmentName,0))&"*",Output,"<>""OK"))
E =IF(NOT(ISERROR(SEARCH("OK",$A2))),SUMIFS(Spend,Output,"*"&INDEX(SegmentID,MATCH($A2,SegmentName,0))&"*"),SUMIFS(Spend,Output,"*"&INDEX(SegmentID,MATCH($A2,SegmentName,0))&"*",Output,"<>*OK*"))
The RAW Sheet looks like (Sheet 2)
A B C D E
Output Ranking Amount Revenue Spend
Random_1234_Random 15 2 5000 550
Random_5678_Random 30 3 6895 450
Random_9102_Random 45 1 4400 350
Random_555_Random 60 4 4000 300
The Lookup Sheet Looks like (Sheet 3)
A B
Segment ID
Carbon 1234
Oxygen 5678
Minerals 9102
Carbon 555
The problem that I have right now is, it will only grab the first Carbon with ID 1234, and not grab the second Carbon with ID 555 at all resulting in:
A B C D E
Segment Ranking Amount Revenue Spend
Carbon 15 2 5000 550
The end result that I would like is for it to grab ALL Carbon with any ID and add all of them together as below into a single line. (RAW sheet, Random_1234_Random + Random_555_Random)
A B C D E
Segment Ranking Amount Revenue Spend
Carbon 75 6 9000 850
Any help would be great and if I need to provide more detail please let me know!
Thank you in advance,
Maykid

Resources