Excel Formula to DAX: How to Reference Previous Row - excel

I'm trying to create a new column in Power BI Desktop which replicates a basic Excel formula which seems simple enough.
The Excel formula which I am trying to replicate is in column D:
=IF(B2=B1, 0, C2)
So that the returned column should look like column D.
Excel Data Example:
I have the same data structure in Power BI and would like to create a new column to replicate column D in Excel, but I can't find a way to replicate the formula in DAX where it calls for :
B2=B1
Power BI Data Example:
Thanks!

The issue here is that DAX doesn't do relative references between the rows in the same way since there is no inherent ordering of the table. You'll have to use the index column in order to find the appropriate row.
In this case, what you can do it look up the [Cage No] in the row where the [Index Order] is one less than in the current row:
Reported Size = IF(
LOOKUPVALUE(
Table1[Cage No],
Table1[Index Order], Table1[Index Order] - 1
) = Table1[Cage No],
0,
Table1[Size] )

Related

Excel Formula to check value of two columns and return specific cell values

I’m trying to create a formula that will check two columns (L and M) for specific values (Active and Disabled) respectively. If Active is found in Column L and Disabled is found in Column M, it gives the Value of Column E.
I’ve tried using nested If statements with index with no luck. I have a table that has over 5k rows I need it to check and spit out the results in a non empty cell table format so vlookup won’t work. I’m at a loss. I’m thinking this will need to be an array but am concerned about the size of the raw data I’m working with.
With Excel 365, you can do:
=FILTER( E:E, L:L&M:M = "ActiveDisabled" )
to give all instances as a spill range.
If there will only be one instance and you want the first instance, you can do:
=INDEX( E:E, MATCH( "ActiveDisabled", L:L&M:M, 0) )

Which method i need to use?

I have the data table:
And i have the choose table:
I need some code, that can sum value by (for example :
if cells in choose table is not empty, then sum values from rows, where direction = 'East' and city = 'NY' and month = 10. (return 1)
If cells direction and month is empty, then sum values from rows,where city = 'NY' . return (4).
And etc...
If cells in choose table is empty, then sum values without limitation. (return 15).
This can be done with a SUMIFS formula. There is no need for VBA.
I'm going to assume the 'Choose' table is on another sheet in your real world, but the formula works if it's on the same sheet - I just wouldn't do it that way due to potential circular references if columns overlap.
Formula:
=SUMIFS(D:D,A:A,IF(B10="","*",B10),B:B,IF(B11="","*",B11),C:C,IF(B12="",">0",B12))
Basically, it replaces empty entries with a wildcard (where it's text) and with ">0" where the column is numerical. Tweak as you see fit.

Excel Pivot Table IF Statement

What I'm trying to do is, I have on the sheet of multiple dates (report dates) in column A and a transition date column in column E. From B - D I have numbers in each column corresponding with actual price then fees. What happens in this is sheet is reports come in and get transitioned on a specific date and switches fees from Column C (Before Transition Date) to Column D (After Transition Date). What I want to do with the Pivot table is this:
I want to be able to add column B & C but when the item hits the transition date to switch over to adding B & D and continues the SUM of it all. My thought process is it would have to do a IF statement, something along the lines of
IF(Transition Date >= Date, Add B & C, IF(Transition Date <= Date, Add B & D),0)
But I could be wrong, just trying to figure this part out on a Pivot Table. Again I am new with Pivot Tables so if my terminology is incorrect please correct me.
If i have understood you correctly you want to add a calculated field to your pivot.
Say your data looked like this Range("A1:E4") in image:
You would add a calculated field to the pivot (Starting row 13 in image) using the following formula:
=IF(Date < TransitionDate, Cost + 'Ore Fee', Cost + 'Mineral Fee')
This equates to:
=IF(ReportDate < TransitionDate, ColB + BeforeTransitionDate, ColB + AfterTransitionDate)
Your mileage on layout may vary as i am using an old mac which is not ideal for pivottables and i have hidden some irrelevant pivot fields.
Windows machine i think you add a calculated field by selecting a cell inside the pivot, the going to Analyze -> Fields, Items, & Sets -> click the little down arrow -> Calculated Field. Example here
Layout of pivot:
Where to enter calculated field:
And comparing with in sheet formulas without pivottable see Column H rows 14:16 (formula i gave from the comments):
There seems to be no need for a PivotTable here, as you can create an additional column (say, ActualPrice) that conditions on summing columbs B & D or C & D:
In the above example, TransactionDate and TransitionDate were made with =DATE(2017,12,randbetween(1,31)), with conditional formatting highlighting the TransactionDates that are on-or-after/before TransitionDate in red/green. The ActualPrice is calculated as =IF(A2<E2,B2,C2)+D2.
If you want you can create a PivotTable to aggregate all of this, or just return the total sum in some cell (outside Column F) as =SUM(F:F).

excel vlookup in tables - search for row and report value in column using column name

I have a database table that is structured like this
SAP Number, FOW, WD, DG
In a different sheet I am using data from this list
It looks like this
SAP Number, WD, DG, ....
The SAP Number is given, WD and DG are searched using VLOOKUP
=VLOOKUP($A2,Objektive!$B:$M,10,FALSE)
This however is difficult to read and could be solved much easier.
I know that the search value is in $A2, and I know that the value is in a tabular named "WD".
Is it possible within Excel to search for the Row similar ot VLOOKUP, but use the column using the column name as [#[WD]]
Here some peudo-code
=VLOOKUPTABLE($A2,Data[#[WD]],FALSE)
If your data table starts from the first column, you can use the COLUMN function to determine the index of the column you want to return data from in the table using the name of that column.
=VLOOKUP($A2,Data,COLUMN(Data[WD]),FALSE)
Alternatively you can use Index/Match to directly reference the columns you want to match and return data from.
=INDEX(Data[WD], MATCH($A2, Data[SAP Number], 0))
If you create a table from the Excel (select any of the cells and press Ctrl + T) you gain access to named references which you can use in Excel formulas. Just make sure your data contains headers.

Count of distinct values with several criteria

I want to be able to count distinct values with several criteria. As presented here below on the screenshot, I would like to know, how many POs do I have per month depending on the PO type (service or material).
Initial data:
Desired result:
All the data is in text format.The difficulty is that the initial data table will be updated every month (I will be adding lines), so the formula has to consider possible updates. Pivot table will not work for me.
The solution I'm offering involves one helper column, but you'll be able to use it quite flexibly. I make the following assumptions:
You can add columns to your original table
The table starts in cell A1
Steps:
Convert your data range into a dynamic excel table (select the range, press Ctrl+t, tick "Table Has Headers" - you get a blue table as a result). This way if you add more data, formulas will drag themselves down.
type "HELPER" into cell D1, and "Number of POs" into cell E1 - this will create 2 new columns (they should turn blue like the rest of table)
paste this formula into cell D2:
=[#[Month/Year]]&[#[Mate/Serv]]
Paste this formula into cell E2:
=SUMPRODUCT(
( [HELPER]=[#[Month/Year]] & [#[Mate/Serv]] ) /
COUNTIFS([Month/Year],[Month/Year],
[Mate/Serv],[Mate/Serv],
[PO No.],[PO No.]
)
)
Add a pivot table, put Number of POs into values area, it will sum values by default.
As in this picture:
Right-click inside the values area, select Summarise Values By.. => MAX (as in second picture below).
You will see that values in pivot have changed.
this way you won't have to manually maintain the header in your summary table as pivot table will pick up any new entries/months.
IF YOU WANT TO STICK TO YOUR ORIGINAL DESIGN:
The same formula can be used (with minor change to add table name to column names), but it still needs the table and helper - follow steps 1-3. Make sure you're set up is as in picture below:
=SUMPRODUCT(
( Table2[HELPER]=H$1&$G2 ) /
COUNTIFS(Table2[Month/Year],Table2[Month/Year],
Table2[Mate/Serv],Table2[Mate/Serv],
Table2[PO No.],Table2[PO No.]
)
)
If anyone would like to change my formula to avoid creating a helper, please do.
I created a dummy table so your results won't be like mine.
How to test:
Copy your data on a new sheet
Go to Data menu ribbon => Remove Duplicates
Pivot the remainder
Put met/serve into rows, month/year into columns, Helper into Values.
That will give you a count you should expect.
Assuming your data is in Columns J:L, you can add a helper column in M, entering this formula in M2 and dragging down:
=COUNTIF(J:J,J2)
Then, assuming your desired result table is in columns N:P enter this formula in O2:
=COUNTIFS($K:$K,O$1,$L:$L,$N2,$M:$M,">0")-MAX((COUNTIFS($K:$K,O$1,$L:$L,$N2,$M:$M,">1")-1),0)
You can then drag this cell over to column P and drag them both down.
This also assumes you Desired result table headers are formatted as text as well.

Resources