Power Pivot previous value - excel-formula

I have the following table & data that can be seen in Excel PowerPivot
item, timeframe, total
1, 1, 15
1, 2, 20
1, 3, 15
2, 1, 10
2, 2, 11
2, 3, 10
While I can easily get the last timeframe, I need to get the previous timeframe's total like:
item, timeframe, total, last_timeframe, last_timeframe_total
1, 1, 15, 0, 0
1, 2, 20, 1, 15
1, 3, 15, 2, 20
2, 1, 10, 0, 0
2, 2, 11, 1, 10
2, 3, 10, 2, 11
I've tried a calculate formula, but that didn't seem to work and only returns blanks. =CALCULATE(SUM(MyTable[total]), MyTable[timeframe] = EARLIER(MyTable[timeframe]) - 1)

EARLIER() doesn't understand any sort of ordering of rows.
EARLIER() refers to nested row contexts.
What you actually want is LOOKUPVALUE() here, which matches the values in specified fields with search criteria you provide, and returns the value that exists for the row which matches those criteria.
Based on your sample it looks like [Timeframe] is a one-incremented index for each item. If this assumption is not true, LOOKUPVALUE() is probably not the function you want.
last_timeframe_total =
LOOKUPVALUE(
MyTable[total]
,MyTable[item] // This is the field we're searching
,MyTable[item] // This is the value to search for in the field
// identified in argument 2 - this evaluates
// in the current row context of the table
// where we are defining last_timeframe_total
// as a calculated column.
,MyTable[timeframe] // The second field we are searching.
,MyTable[timeframe] - 1 // Similar to argument 3.
)
This will give you the value for the prior timeframe for the current item.
Ninja edit: Forgot to mention that Power Pivot isn't really the layer to be doing this sort of work in. Lookups and this sort of data shaping are better done in your ETL from transactional sources. If this is not possible, then it's better done in the query to populate Power Pivot than in Power Pivot. Power Query is a good tool to use for this sort of transformation that easily fits into the Microsoft ecosystem, being another Microsoft add-in for Excel.
Power Pivot is an analytical database optimized for aggregations. In general, if you ever find yourself thinking "for every row," it's a sign that what you're trying to accomplish is probably better suited for a different layer of the BI solution.

Related

Is there an EXCEL formula that counts how many times Excel data 'switches' between binomial coding (i.e., data coded as 1 or 2)?

Folks, would really appreciate some help;
If I have the following values of data in Excel (3 examples below) where data is coded as 1 or 2, does anyone know an Excel formula which can count how many 'switches' occur in the sequence of values? What I mean by a 'switch', is when the 1's switch to 2's, and vice versa (when the 2's switch to 1's).
For example;
1, 1, 1, 1, 1, 1, 1, 2, 2 (there is 1 switch here)
1, 2, 2, 1, 1, 1, 1, 1, 1 (there are two switches here)
1, 2, 2, 1, 1, 1, 1, 2, 2 (there are three switches here)
So far, I am able to use the following formula (see below) to see IF there is a switch at all in the sequence (from 2 to 1 for example). But now I am trying to calculate how many switches are occurring in the sequence, and not IF a singular switch is occurring. So I think I possibly need to use a COUNT formula, instead of a FIND formula?
=IF(ISERROR(FIND("21",TEXTJOIN("",TRUE,[data range of a row]))),FALSE,TRUE)
Any help is appreciated.
If you have your data in cells A1 to I1, then use this formula:
=SUM(ABS(B1:I1-A1:H1))
I've tested this with your three inputs and it produces the expected answers.
If you would have access to FILTERXML (I believe from Excel 2013 onwards) you could use:
Formula in B1:
=COUNT(FILTERXML("<t><s>"&SUBSTITUTE(A1,", ","</s><s>")&"</s></t>","//s[following::*[1]!=.]"))
For your understanding; the xpath expression //s[following::*[1]!=.] will return all (child)nodes where the first following node is different then the current one. Then COUNT will actually count these returned numbers. Note that this will return 0 when no change occurs in your string.

Is there an Excel Formula which returns at WHAT POINT in an active string of data, data changes form (from 1 to 2)?

Folks, REALLY would appreciate help <3
If I have the following strings of data in Excel (3 examples below) where data is coded as 1 or 2, does anyone know an Excel formula which can tell me WHEN in the sequence the data switches from 1 to 2? i.e., in the example, at the 4th, 2nd, and 8th point/column along, out of the 9.
1, 1, 1, 2, 2, 2, 2, 2, 2.
1, 2, 2, 2, 2, 2, 2, 2, 2.
1, 1, 1, 1, 1, 1, 1, 2, 2.
So far, I am able to use the following formula (see below) to see IF there is a change from 1 to 2 in the sequence, but now need to know WHEN the change occurs.
=IF(ISERROR(FIND("21",TEXTJOIN("",TRUE,[data range of a row]))),FALSE,TRUE)
The Find() function actually return the position of the target string.
Instead of IF(ISERROR(..., use
=iferror(FIND("21",TEXTJOIN("",TRUE,[data range of a row])),FALSE)
and better link the previous post for system integrity purpose.
You just need to find the number '2' by using:
=ROUNDUP(FIND(2,A1)/3,0)

How to get the second largest value in a column

Recently I discovered the LARGE and SMALL worksheet functions, one can use for determining the first, second, third, ... larges of smalles value in an array.
At least, that's what I thought:
When having a look at the array [1, 3, 5, 7, 9] (in one column or row), the LARGE(...;2) gives 7 as expected, but:
When having a look at the array [1, 1, 5, 9, 9], I expect LARGE(...;2) to give 5 but instead I get 9.
Now this makes sense : it seems that the function LARGE(...;2) takes the largest entry in the array (value 9 on the last but one place), deletes this and gives the larges entry of the reduced array (which still contains another 9), but this is not what one might expect intuitively.
In order to get 5 from [1, 1, 5, 9, 9], I would need something like:
=LARGE_OF_UNIQUE_VALUES_OF(...;2))
I didn't find this in LARGE documentation.
Does anybody know an easy way to achieve this?
If you have the new Dynamic Array formulas:
=LARGE(UNIQUE(...),2)
If not use AGGREGATE:
=AGGREGATE(14,7,A1:A5/(MATCH(A1:A5,A1:A5)=ROW(A1:A5)),2)
This is a bit of a hack.
=LARGE(IF(YOUR_DATA=LARGE(YOUR_DATA,1),SMALL(YOUR_DATA,1)-1,YOUR_DATA),1)
The idea is to (a) take any value in your data that is equal to the largest element and set it to less than the smallest element, then (b) find the (new) largest element. It's OK if you want the 2nd largest, but extending to 3rd largest etc. gets progressively uglier.
Hope that helps

Using result of an Excel array function in a calculation

I am attempting to count instances of a particular value in Excel, from the last instance of a prior value.
Assume a vertical list starting in cell A1: 1, 2, 3, 4, 5, 4, 5, 3, 4, 5, 2, 3, 4, 3, 4, 2, 3, 4, 5
I can use an array function in, say B14 (A14 value: 3), of {=MAX(ROW($1:14)*(A$1:A14=A14-1)) to give me the row number of the last instance of a "2" (row 10).
I can then have, in C15, a function =COUNTIF(OFFSET(A14,0,0,B14-ROW(A14),1):A14,A14), which will count the instances of 3's since the last 2.
The question is: how do I integrate that array function directly into the final formula, so as not to have to waste a column with the interim calculation?
Edit
The list of numbers represents a level of indentation, so the end result will be a compound of these calculations with different offset checking to provide section numbering: 1; 1.1; 1.1.1, 1.2, 1.2.1, 1.2.2, etc
I want a single function that can calculate this entire depth level, without having to waste several columns identifying how many rows above the previous indent layer was defined.
Try in cell B14 this formula array:
{=COUNTIF(OFFSET($A14,0,0,
MAX(ROW($1:14)*($A$1:$A14=$A14-1))
-ROW($A14),1):$A14,$A14)}

Sort Part Numbers by First Character in Excel

I have an excel document with about 12,000 lines of part numbers with pricing information. I think the most intuitive way to sort them would be as follows:
1, 12, 15, 100, 10003, 2, 2002, 20005000, 3, 30, 333, 4, 5, 6, 700000, 800000.
All the numbers that begin with 1 are in the same spot, same with all 2 prefixes, etc. My problem is, excel's default sorting method sorts it like THIS:
1, 20, 30, 40, 100, 150, 200, 250, 500, 1000, 1500, 2000, 10000.
So it sorts them in regular ascending order, which I think makes it more difficult to find your part number, especially as they get super long as some of them do.
I don't have a lot of resources outside of excel at my workspace, so I would like to stick to using that. My knowledge of excel also isn't too impressive, so please treat me like an idiot!
To make thing easier to describe, let's assume that your list of part numbers is in column A, and that you have a blank column B.
In cell B2 enter this formula:
=TEXT(A1,"#")
Copy that down as far as your list goes.
Now, selecting both columns, Custom Sort by column B.
That's it.
Create a new column called SortOrder with the following formula:
=VALUE(LEFT(A2,1))
Then go to Home tab > Sort & Filter > Custom Sort.... Sort first by SortOrder, then by Value:
With data in column A like:
In B1 enter:
=CHAR(1) & A1
and copy down. Then sort cols A & B by B to get:
Is this "alphabetic-style" sort closer to what you need??

Resources