Power BI: Compare two columns in a table visual - excel-formula

I am trying to compare two columns in Power BI and only color when there is a difference from one column to another.
Like this:
The pictures are from:
https://community.powerbi.com/t5/Desktop/Compare-two-columns-in-a-table-visual/m-p/272761#M122058
I saw the solution there (the link above), but since it was asked two years ago, I wanted to check if there is a better solution or color formatting I am not aware of.
I have to compare over 70 pairs of columns. That is why I was checking if there is an alternative that does not require to create a column per pair
Or how would you approach it?
I really appreciate any help you can provide.

Conditional formatting has improved. You can now conditionally format a column by other fields. So you can define a difference measure and use that:
Diff = IF ( SUM ( Sales[Sales - DB] ) = SUM ( Sales[Sales - File] ), 1, 0 )

To add to this.
Is there a way to compare two string columns in the matrix table using "new measure"
= if(Column A = Column B, True, False)

Related

EXCEL - Dual VLOOKUP and Interpolation

I have a table on Excel with data as the following:
Meaning, I have different JPH based on the %SMALL unit and the number of active stations.
I need to create a matrix like the following (with %SMALL on horizontal and STATIONS on vertical axes):
And the formula for each cell should:
Take the input of Stations (column "B")
Check, for that specific Stations number, the amount of data on the other table (like make a filter on STATIONS for the specific number)
Perform an VLOOKUP for checking the JPH based on the %SMALL value on row 2
Interpolate for the exact JPH value, if not found on table
For now, I was able to create the last part (the VLOOKUP and the interpolation), with the following:
=IFERROR(VLOOKUP(C2;'EARLY-STATIONS'!$F:$H;3;FALSE);AVERAGE(OFFSET(INDEX('EARLY-STATIONS'!$H:$H;MATCH(C2;'EARLY-STATIONS'!$F:$F;1));0;0;2;1)))
The problem I'm facing is than with this, the calculation is not checking the number of stations, so the Iteration is not accurate.
Unfortunately I cannot use VBA macros to solve this.
Any clue?
This is an attempt because more clarity is needed in terms of all possible scenarios to consider, based on different input data and how to understand the "extrapolation" process. This approach understands as extrapolation the average of two values (lower and greater), but the idea can be customized to any other way to calculate it. Per tags listed in the question I assume there is no Excel version constraint. This is O365 solution:
=LET(sm, A2:A10, st, B2:B10, jph, C2:C10, smx, F1:J1, sty, E2:E4, NULL, "",
GETLk, LAMBDA(x,y,mode, FILTER(jph, (st=y)
* (sm = INDEX(sm, XMATCH(x, sm, mode))), NULL)),
GET, LAMBDA(x,y, LET(f, FILTER(jph, (jph=GETLk(x,y, 1))
+ (jph=GETLk(x,y, -1)), NULL), IF(#f=NULL, NULL, AVERAGE(f)))),
HREDUCE, LAMBDA(yi, DROP(REDUCE("", smx, LAMBDA(ac,x,
HSTACK(ac, GET(x, yi)))),,1)),
DROP(REDUCE("", sty, LAMBDA(ac,y, VSTACK(ac, HREDUCE(y)))),1))
The above formula spills the entire result, I don't think for this case you can use a LOOKUP-like function.
Here is the output:
The highlighted cells where the average is calculated.
Explanation
The main idea is to use DROP/REDUCE/HSTACK/VSTACK pattern to generate the grid. Check my answer to the following question: how to transform a table in Excel from vertical to horizontal but with different length on how to apply it.
We use two user LAMBDA functions to abstract some calculations:
GETLk(x,y,mode), filters jph name based on %SMALL and Stations columns values, based on input values x (x-axis value from the grid), y (y-axis value form the grid) respectively. The third input argument mode, is for doing the approximate search in XMATCH (1-next largest, -1 next smallest). In case the value exist in the input table, XMATCH returns the same value in both cases.
GET(x,y) has the logic to find the value or if the value doesn't exist to calculate the average. It uses the previous LAMBDA function GETLk. We filter for jph values that match the input values (x,y), but we use an OR condition in the FILTER (+), to select both lower or greater values. If the value exist, returns just one value otherwise two values are returned by FILTER (f). Finally if f is not empty we return the average, otherwise the value we setup as NULL.
HREDUCE: Concatenate the result by columns for a given row of the grid. Check the referred question for more information about it.

sum two values of dimension but show the result only in one of them in PowerPivot / DAX

It might be a easy one but I cannot solve it so please advice.
I would like to add up two months in a measure (like the current and the previous one) but when I represent the figure in a pivot table I want to show the result only in a current month.
Here is the formula I tried:
CALCULATE([Revenue],FILTER(Tbl_Period,Tbl_Period[Month_nr] = 3)) +CALCULATE([Revenue],FILTER(Tbl_Period,Tbl_Period[Month_nr] = 4))
But logically when I represent it in pivot table the revenue is appear under the two months:
See the picture of the result of the below formula:
What I want to achieve instead is the following:
See the picture of the aimed situation:
I already managed to solve with the ALL function to show the value in all the months but not only for July as I wanted. Here is the formula for that one:
Revenue:
=CALCULATE([Revenue],FILTER(ALL(Tbl_Period[Month_nr],Tbl_Period[Period_id]),Tbl_Period[Month_nr] = 3)) +CALCULATE([Revenue],FILTER(ALL(Tbl_Period[Month_nr],Tbl_Period[Period_id]),Tbl_Period[Month_nr] = 4))
Thanks in advance for the help!
I'm a little bit blind, since you haven't mentioned the structure of your Tbl_Period table.
There are many helpful functions built into dax that can save you a lot of work if you have a date table. One that would be relevant here is PREVIOUSMONTH().
Working with just the information you have provided, you can get a measure to show the previous month's revenue like this:
LastMonthRevenue: = CALCULATE([Revenue], FILTER(ALL(Tbl_Period), Tbl_Period[Month_Nr] = MAX(Tbl_Period[Month_Nr]) - 1))
and then
TwoMonthsRevenue: = [Revenue] + [LastMonthRevenue]

Minimum & Maximum - having two compulsory conditions

I have 3 columns with the following name:
name1, name2 and value.
I want to obtain in another sheet two tables having two compulsory condition (min and max calculated using name1 and name2):
the name of the first table to be taken from the column with name2 and this table has two partition.
the first partition named max, is calculating the max for 30_-20, 40_-20, 50_-20, 30_22, 40_22, 50_22, 30_60, 40_60, 50_60 and second partition named min, is calculating the min for 30_-20, 40_-20, 50_-20, 30_22, 40_22, 50_22, 30_60, 40_60, 50_60.
What I want to say can be viewed in the following picture.
I need this for my job, and I don't know anything about macros. I think it will be necessary to learn macros.
Given a layout on a single worksheet similar to your sample image.
    
The standard formula(s) for G3, I3, M3 and O3 are:
=MAX(INDEX($C$2:$C$999*($A$2:$A$999=H3)*($B$2:$B$999=H$1), , ))
=MIN(INDEX($C$2:$C$999+(($A$2:$A$999<>H3)+($B$2:$B$999<>H$1))*1E+99, , ))
=MAX(INDEX($C$2:$C$999*($A$2:$A$999=N3)*($B$2:$B$999=N$1), , ))
=MIN(INDEX($C$2:$C$999+(($A$2:$A$999<>N3)+($B$2:$B$999<>N$1))*1E+99, , ))
Fill down as necessary. It is usually easier to reference a cell containing a value (e.g. 30_-20) than repeatedly hardcoding the value into a variety of similar formulas. I've used H3:H5 and N3:N5 for the column A values.
How it Works:
See MINIF, MAXIF and MODEIF with Standard Formulas.

How to convert a matrix to a single column using Excel

I have the following matrix in Excel:
3 Columns: A, B, C
Row 1: a b c
Row 2: d e f
Row 3: ghi
What I need is a single column with all these values. The result should look like that:
a
b
c
d
e
f
g
h
i
The TRANSPOSE function doesn't work for that case. I tried out the INDIRECT function, but did not find a solution. I would rather prefer to handle it with standard Excel formulas than with a makro.
Any ideas?
Say we have:
In E1 enter:
=INDEX($A$1:$C$3,ROUNDUP(ROW()/3,0),IF(MOD(ROW(),3)=0,3,MOD(ROW(),3)))
and copy down to get:
Using similar formulas you can map any two dimensional table into a single row or single column. It is equally easy to map a single column or row into a table.
The answers above are quite good, but IMHO, the solution provided by Chip Pearson here (http://www.cpearson.com/excel/TableToColumn.aspx), is superior is most respects since it immediately/automatically:
1) Determines the Row/Col delim values on its own, and immediately works for rectangular, e.g. above one must explicitly enter 3 for num Cols and 3 for num Rows, and must also figure out which is which. Whereas Pearson's solution does this automatically (eg. rmf's comment/concern above).
2) Pearson provides both variants for Col-ordered and also Row-ordered.
For a generalized approach that will create an array, you can use:
=LET( Matrix, $A$1:$C$3,
rM, ROWS( Matrix ),
cM, COLUMNS( Matrix ),
cells, SEQUENCE( 1, rM * cM, 0 ),
INDEX( Matrix, INT( cells / cM ) + 1, MOD( cells, cM ) + 1 )
)
While this takes advantage of the LET function, it is used for readability. For those not using Excel365, it is possible to do this without LET, but it is just painful to read .
If you need the result to be delivered as a column, change the order of arguments in SEQUENCE
= LET( Matrix, $A$1:$C$3,
rM, ROWS( Matrix ),
cM, COLUMNS( Matrix ),
cells, SEQUENCE( rM * cM, 1 , 0 ),
INDEX( Matrix, INT( cells / cM ) + 1, MOD( cells, cM ) + 1 )
) )
Of course, A1:C3 can be any arbitrarily shaped array.
This was already covered in Item 2 from the very general
Excel: Formulas for converting data among column / row / matrix :
The top cell of your target range (say, $H$1) should contain
=INDEX($A$1:$C$3,INT((ROW()-ROW($H$1))/3)+1,MOD(ROW()-ROW($H$1),3)+1)
where $A$1:$C$3 cotains your source data.
Copy the formula downwards as needed.
You could also use
=OFFSET($A$1,INT((ROW()-ROW($H$1))/3),MOD(ROW()-ROW($H$1),3))
as metioned in the referred article.
I suggest you to check Excel unpivot option to perform your task.
Select your matrix
Go through the Get & Transform section in the Data tab and click From Table/Range
In the new Power Query Editor select the columns you want to unpivot
Go through the Any Column section in the Transform tab, click the arrow nearby Unpivot Columns and choose the best option (if you follow the step 3 you can click Unpivot only selected columns)
Close & Load
This process is useful especially for complex matrices.
Check the above link for further information
With the current version of Excel, I realized operations with matrix has improve greatly. For converting a matrix to a single column, I suggest combining INDEX and SEQUENCE Functions in the following way:
Matriz_to_Column_Formula:
=INDEX(matrix_range,
TRUNC((SEQUENCE(ROWS(matrix_range)*COLUMNS(matrix_range),1,0,1))/COLUMNS(matrix_range)+1,0),
MOD(SEQUENCE(ROWS(matrix_range)*COLUMNS(matrix_range),1,0,1),COLUMNS(matrix_range))+1)
If you want to remove empty spaces:
=FILTER(Matriz_to_Column_Formula, Matriz_to_Column_Formula<>"")
Here is a short explanation:
Index requires 3 things: matrix source, row and column. The row and column you calculate each time allows index to point at that specific element in your matrix source. Index would give you as many elements as you require, which is Rows*Columns.
Let me expand on Rows and Columns using SEQUENCE to navigate by each element:
Row:
TRUNC((SEQUENCE(ROWS(matrix_range)*COLUMNS(matrix_range),1,0,1))/COLUMNS(matrix_range)+1,0)
The formula SEQUENCE will generate as many elements as your matrix has, starting from 0 until Rows*Columns-1, and the division by columns will point only at a specific row. The +1 is used because sequence starts at 0 and INDEX Row element works from 1 to n. TRUNC is used only to get a integer number of the row.
Column:
MOD(SEQUENCE(ROWS(matrix_range)*COLUMNS(matrix_range),1,0,1),COLUMNS(matrix_range))+1
Here you apply the same principle, using SEQUENCE to generate as many elements as your matrix has from 0 to Rows*Columns-1, and by getting the remainder between the sequence and Columns you will point to the column you need to.
Alright, this is my first post, hope this helps anyone. Thanks for you help in many occasions!
Example in Excel:
Matrix to Single Column - Example in excel
Opt 1:
=INDEX(C3:E7,
TRUNC((SEQUENCE(ROWS(C3:E7)*COLUMNS(C3:E7),1,0,1))/COLUMNS(C3:E7)+1,0),
MOD(SEQUENCE(ROWS(C3:E7)*COLUMNS(C3:E7),1,0,1),COLUMNS(C3:E7))+1)
Opt 2:
=IF(INDEX(C3:E7,TRUNC((SEQUENCE(ROWS(C3:E7)*COLUMNS(C3:E7),1,0,1))/COLUMNS(C3:E7)+1,0),MOD(SEQUENCE(ROWS(C3:E7)*COLUMNS(C3:E7),1,0,1),COLUMNS(C3:E7))+1)="","",INDEX(C3:E7,TRUNC((SEQUENCE(ROWS(C3:E7)*COLUMNS(C3:E7),1,0,1))/COLUMNS(C3:E7)+1,0),MOD(SEQUENCE(ROWS(C3:E7)*COLUMNS(C3:E7),1,0,1),COLUMNS(C3:E7))+1))
Remove Empty:
=FILTER(INDEX(C3:E7,TRUNC((SEQUENCE(ROWS(C3:E7)*COLUMNS(C3:E7),1,0,1))/COLUMNS(C3:E7)+1,0),MOD(SEQUENCE(ROWS(C3:E7)*COLUMNS(C3:E7),1,0,1),COLUMNS(C3:E7))+1),
INDEX(C3:E7,TRUNC((SEQUENCE(ROWS(C3:E7)*COLUMNS(C3:E7),1,0,1))/COLUMNS(C3:E7)+1,0),MOD(SEQUENCE(ROWS(C3:E7)*COLUMNS(C3:E7),1,0,1),COLUMNS(C3:E7))+1)<>"")

PivotTable - Calculate value depending on combination of row labels

WARNING - Using Excel 2011 for Macs, inexperienced user
Hi All,
I have a sheet in Excel with a bunch of categorical fields and some numerical ones as well. Let's say it looks like the following:
I would like to make a pivot table that will display the average click rate (avg_click_rate) of the unique combinations of [year, region], i.e. the combinations of fields in the pivottable's rows section.
For example, the avg_click_rate of [years=5] is:
(0.5*10)/(10 +5 ) + (0.6*5)/(10+5) = 0.53
while the avg_click_rate of [region=north] is:
(0.6*5)/(5+20) + (0.2*20)/(5+20) = 0.28
and the avg_click_rate of [years=5, region=south] is:
(0.5*10)/10 = 0.5
I know I have to make a custom Calculated Field to do this, but for the life of me I cannot figure out how to code the formula. Any help would be seriously, seriously appreciated.
To be clear, my formula would be:
SUM{ (click_rate * number_members) / SUM{number_members} }
where the numerator is a single value for each row included in the unique combination of [year, region], while the denominator is a constant - the total number_members for the unique combination of [year, region].
You should create a new column in your source table:
product = click_rate * number_members
And then create a Calculated Field in the pivot table:
CF = product / number_members
Using AVERAGEIFS:
AVERAGEIFS($C$2:$C$9,$A$2:$A$9,A2,$B$2:$B$9,B2)
EDIT:
You can add up to 127 conditions, see: AVERAGEIFS function
Criteria_range1, criteria_range2, … are 1 to 127 ranges in which
to evaluate the associated criteria.

Resources