EXCEL: How to automatically add serial number in Excel Table using formula that is immune to filtering / sorting? - excel

I want to create an Excel Table where the first column is the "SL" (serial number) column that starts from 1 and then increases by 1 for each subsequent entry. I want the serial number to automatically increase as I add more rows to the table.
I have tried using all manners of "=ROWS" functions, all manners of "=COUNTA" functions, and all other functions used in tutorial that I found in the web. None of them are immune from sorting or filtering. That is, if I sort the "Name" column from A to Z, the serial number that was assigned to its respective row entry changes because of how these formulae are written. For example:
This is the Original List. As you can see, Dragon Fruit's serial number is 1. I have used the "=COUNTA(B$2:[#[NAME]])" function in this example.
As you can see, when I sorted the "Name" column from A to Z, Dragon Fruit's serial number went from 1 to 2, Acai went from 4 to 1, Guava went from 9 to 3, and so on. But I want the serial numbers to be static and locked to their corresponding "Name".
Is this possible to do in Excel without manually typing the numbers in the SL column?

Good question and a tricky situation to deal with. I'm not sure if the question is better suited for SuperUser though.
The trick here is to somehow use absolute cell-references instead of relative ones. As you have now experienced, Excel filters won't work well with relative references. However, manually adding absolute references is not what we want to do.
To mimic the absolute cell-reference behaviour you can preceed the row reference with a sheet-reference which magically should counter the normal formula-behaviour and turn them into actual absolute cell-references:
Formula in A2 (which will auto-fill the 'SL' column):
=ROW(Sheet1!A1)
Data when filtered A-Z on 'Name':
Data when tabbed a new row:

You could use PowerQuery in Excel to add an index in front.
Remove the ID from your source.
Make your source a table
Import into PowerQuery and add an index
Load the output to another sheet. In this sheet you can filter and sort and everything you want.

Related

Excel multilevel array formula with partial string matches to sum resultant cells

I've been trying to sort this for over a day now without much luck. I have successfully used SUMIFS, INDEX, MATCH, COUNTIF, "--" etc array functions previously and am not a novice, but also not an expert on these. I can't seem to weave these together correctly, and likely on an altogether incorrect path.
Basically, I am trying to aggregate data from multiple spreadsheets, requiring a mapping of various items (rows) into a canonical form for summing.
The image here shows a representative, but simplified version of my quest. Each "region" on this example spreadsheet (Final..., Mapping, DataSet1, DataSet2) is actually in different spreadsheets, and there are several sheets with 50-150 rows in each xlsx.
Note that the names in Column B are quite arbitrary (meaning not all P1's have an 'x' pattern, like shown here as x1, x2, etc. Do not rely on any pattern in the names, except the x, y , z in the Mapping table are substrings (case insensitive, trailing match) of the names in Column B in the DataSets.
And in the image, the Final Result Table (summed manually) is what I want to compute via(an array) formula: A single formula would be ideal (given I have many spreadsheets from which the monthly data is being pulled from, so I can't readily modify but can create an interim spreadsheet if required, so open to helper columns or helper rows).
Here's the process - For each name (B3-B5) in the Final Result Table, I want to sum the name from it's components as follows:
Lookup all the matches in the Mapping Table (so for P1, the formula =IF($C$10:$C$15=$B3, $B$10:$B$15,"") gives {"x1";"";"";"x2";"";"x3"}.
I then want to search each of x1, x2, and x3 in B19:B26 to get rows 21, 22, 24, 25, 26 in DataSet1 and B31:B35 to get row 32 in DataSet2, to then add up the Jan totals into C3. (Effectively,
C3=C21+C22+C24+C25+C26+C32). Same for P2 and P3, and thru Feb, Mar, ...
I am stuck on how to remove blank or 0 or Div0 or such "error rows" from the interim result in 2, and also need to use 2 arrays of different sizes (3 valid rows in example 2 above, ignoring blanks) to search many rows in DataSets. I tried SEARCH("*"&IF($C$10:$C$15=$B3, $B$10:$B$15,""), $B$19:$B$26) but get unexpected results. I have tried to replace text in the interim result {"x1";"";"";"x2";"";"x3"} with TRUE/FALSE, and 1/0, etc. to help with INDEX or MATCH, but am stymied by errors in downstream ("surrounding") formulas.
Thanks in advance.
Here is a solution without resorting to nasty (imo) CSE formulas.
= SUMPRODUCT($C$19:$F$26*(COUNTIFS($B$10:$B$15, RIGHT($B$19:$B$26,2),$C$10:$C$15,$B3)>0)*($C$18:$F$18=C$2))
+
SUMPRODUCT($C$31:$F$35*(COUNTIFS($B$10:$B$15, RIGHT($B$31:$B$35,2),$C$10:$C$15,$B3)>0)*($C$30:$F$30=C$2))
There is one SUMPRODUCT for each data set. If possible, it would be better to put all your data sets into a single table with a column identify which data set it is a part of.
The way it works is to takes each values in your data set and multiplies it by whether the 2 right most character appear in your mapping table for that P code, multiplied by whether the value is in the correct month. So it returns 0 if either of those conditions are false. Then returns the sum.
UPDATE IN RESPONSE TO OP COMMENTS
If, the X,Y, Z codes are not always 2 digits but the first part is ALWAYS 8 digits, you can easily amend the:
RIGHT($B$19:$B$26,2)
to be:
RIGHT($B$19:$B$26,LEN($B$19:$B$26)-8)
Making the formula for the first data set:
=SUMPRODUCT($C$19:$F$26*(COUNTIFS($B$10:$B$15, RIGHT($B$19:$B$26,LEN($B$19:$B$26)-8),$C$10:$C$15,$B3)>0)*($C$18:$F$18=C$2))
And you can amend for other data sets and simply add them together.
Nice challenge! Are you willing to drop all your tables (DataSet1, DataSet2...) into one spreadsheet, so that we can refer just one single range for each month?
Here's one solution (hopefully a good starting point) - array formula (Ctrl+Shift+Enter):
=SUMPRODUCT(IFERROR(IF(TRANSPOSE(IF($B3=$C$10:$C$15,$B$10:$B$15,""))=RIGHT($B$18:$B$36,2),C$18:C$36,0),0))

Update Excel ListObject header names without breaking pivots

I have an Excel sheet with a very wide table on it. Due to developer friendlyness I'd like to use a certain style of column header naming (much like proper Hungarian notation), where I suffix each header name with "column type" tags. This allows me to easily spot where e.g. apples and oranges are compared. There are also pivot table reports based on this table.
An example to illustrate this: say you have 2 monetary columns, column A being expressed in another currency than column B. The model should thus never combine them without first applying appropriate exchange rates. To spot this I name these columns e.g. Earned - Cur1 and Saved - Cur2. Any calculation like =[#[Earned - Cur1]] + [#[Saved - Cur2]] is illegal, but due to the tags this can be picked up easily in an audit. I have several such tag groups in use already, and they already prevented some errors creeping in.
However...
The file also needs to be distributed to lots of not-so-savvy end users, and they need to fill in this table and refer to some of the outcome columns. Most intermediate columns we already hide, but the column names are now far from being user-friendly (like: fill out Actual - NK/Q1/EC/%, please?).
And this needs to run in Excel 2010.
What are my options?
Option 1
Add an extra row above the table, putting human readable names in there, and just hide the table header row. This works, but not the users can't sort and filter the table anymore, so that's a no-go.
Option 2
Augment option 1 by prepending a newline to each column name, and make the table header row 1 character high. The header cells would still be there to drive sorting and filtering and the users have human readable names in the row above. The actual header cells would appear like 'empty' buttons. Could work, but then the complex formulas become unreadable due to all the newlines from the column names all over the place.
Option 3
Add a macro that switches the headers in the table by alternative headers in another row above the table. The macro should be ran just before sending out the file to the users, and ran again when they return them filled in and all. I happily coded this option into the file, and it works wonderfully! But then I realized this (and thus option 2 as well) breaks all the derived pivot tables, since Excel links the data by the names used in the table - update the name, and that section of the pivot will be dropped...
I'd really like the option of having our development-oriented column names in there when we ourselves work with the file, but being able to switch out the headers when needed. And of course without rebuilding all the pivots after each such switch.
An opening here would be that pivots seem to only drop the columns once they're refreshed. I could use this to update the header names, then do some magic on the pivots to remap their fields, and only then refresh them, but it seems there's no way from within VBA to accomplish that (PivotField.SourceName is read only).
Hopefully someone can think of an alternative, or am I SOL? I'm totally open to other workarounds.
Workaround 1
Insert null-terminating characters in the header names such that they do not show normally in the formulas, but do not show in the table header row. If only it were that simple though... Turns out Excel throws up from a =Char(0)&"abc", and things like =Char(8)&"abc" (tab anyone?) give Unicode replacement characters when pasted into a header cell... (?)
Workaround 2
A last resort seems to be to unzip the excel file, and plough through the xml data to update everything in one go there, then rezip the file. But this code also needs to be executed by less skilled users, and I see too many ifs and buts to make me feel safe using this setup.
Workaround 3
For now I just use a variation on option 2; I have some VBA that 'empties' the header cells instead of prepending a newline to them. By 'emptying' I mean setting the font size to 1, subscript, non-bold, and then make the font color identical to the background color, followed by setting it's row height to the default 14.5. The cryptic names do leak out however; column header cell drop down arrows for sorting&filtering show the cryptic name, as well as the pivot field settings and of course the formula bar when you just click such a cell. But I guess it's the best I can do?
And then again I'm probably just perfectionizing this thing faaar to much :) But from this point on it's about the challenge!
Make sure you Tick the Box "Add this data to the DataModel" when creating your pivot(s)
AFAIK when your Pivots are connected to the Datamodel instead of directly to the Range/Table you can change your column-names in the Table and your Pivot will stay fine. You could even use other names in your Pivot.

Sort AlphaNumeric with structured references in Excel

I Have an Excel Sheet with data that looks like that.
Data
x=1.1
x=11.2
x=10.3
x=1.4
x=2.5;2.6
x=2.1
x=4.7
x=6.8
x=6.2;6.3
x=1.10
What i want to do is, to sort the List that it Looks like that.
DataSort
x=1.1
x=1.4
x=1.10
x=2.1
x=2.5;2.6
x=4.7
x=6.2;6.3
x=6.8
x=10.3
x=11.2
I tried to do that with that Formula
=LEFT(Tabelle1[[#this row];[Data]];2) & TEXT(SUBSTITUTE(Tabelle1[[#this row];[Data]];LEFT(Tabelle1[[#this row];[Data]];2);"");"#0.0#")
But that did not work.
Can someone give me a hint to the right Direction?
Copy the data into a new column say B
If you are using excel 2007 or higher, go to data tab --> text to columns option after selecting your data in B
Here you can choose delimited and it will separate your data into 2 columns.
Then apply the sorting based on this column
In an unused column to the right, use this formula starting in row 2,
=LEFT(A2, 2)&TEXT(--MID(A2, 3, FIND(".", A2&".")-3), "000;#")&TEXT(--MID(A2, FIND(".", A2&".")+1, 99), "000;#")
Fill down as necessary then sort conventionally using the helper column as the primary sort key.
        
If you put a 0 prior to the numbers, that would take care of it.
X=01.1
Or, in a convoluted way, split the column as recommended above, sort the way you want and then reassemble. I would also create a column with the right 1-n sequence, just in case I need to sort in a particular way, but the come back to the original sort.
The Solution to the Problem is:
=LEFT(IF(ISNUMBER(SEARCH(";";A1));LEFT(A1;FIND(";";A1;1)-1);A1);2)&TEXT(--MID(IF(ISNUMBER(SEARCH(";";A1));LEFT(A1;FIND(";";A1;1)-1);A1);3;FIND(".";IF(ISNUMBER(SEARCH(";";A1));LEFT(A1;FIND(";";A1;1)-1);A1)&".")-3);"000;#") &TEXT(--MID(IF(ISNUMBER(SEARCH(";";A1));LEFT(A1;FIND(";";A1;1)-1);A1);FIND(".";IF(ISNUMBER(SEARCH(";";A1));LEFT(A1;FIND(";";A1;1)-1);A1)&".")+1;99);"000;#")

Excel - Nested IF/Nested AND/OR in 'calculated field' option - pivot table

I have the following problem:
A datasheet with a column (HOUR) and another column (AM/PM). Entries in the first column consist of 1,2,3,4,5,6,7,8,9,10,11, or 12, the second column consists of 'AM's or 'PM's. Together they define the time of an incident (regarding the below problem, note that I am not allowed to create a new column in the source datasheet or change existing columns). The below formulas 1.) to 3.) work excellent for getting '1's or '0's for incidents that happened either between 8AM and 4PM, or outside of this time window, as long as I create a new column somewhere.
1.) =IF(AND(A1>=8, A1<=11),IF(B1="AM",1,0),0) + IF(AND(A1>=1, A1<=4),IF(B1="PM",1,0),0) + IF(AND(A1=12),IF(B1="PM",1,0),0)
2.) =--OR(AND(A1>=8, B1="AM", A1<>12), AND(OR(A1<=4, A1=12), B1="PM"))
3.) =--OR(AND(OR(A1={8,9,10,11}),B1="AM"), AND(OR(A1={1,2,3,4,12}), B1="PM"))
However, I want the "1"s to be summarized - without creating an extra column - as calculated field in a pivot table. While excel doesn't accept the 3.) formula at all in the calculated field option, excel accepts 1.) and 2.), but puts out only "0"s in all pivot cells. The below is one of the formulas that puts out only "0"s in the pivot table.
=--OR(AND(HOUR>=8,'AM/PM'="AM",HOUR<>12), AND(OR(HOUR<=4,HOUR=12),'AM/PM'="PM"))
The field value settings don't make a difference, and the fields that are created new with 1.) or 2.) cannot be filtered for "1"s or 0"s, so something must be wrong with the field calculation I guess. Does anybody know what I need to change to make it work? Are there special rules for formulas in pivot tables that apply to formula 1.) and 2.) to make them work?
Thanks for any help on this
I think the limitation is not you, but Excel.
See here for description of what is possible, as well as this question
I tried your code and indeed I see it's not working. Even with a simple if code it doesn't seem to work. I think it's is explicitly called a calculated field because you are only able to calculate the fields in the Sum/Total/Count etc. column.
Have a look at MS, this function is quite limited.
I would try to make another work-around to accomplish your goal.
Microsoft

Display, sort and filter numbers with multiple decimal in excel 2007

I'm using excel 2007.
I've a list of tasks (200-500) that I need to group in different category/section etc (multiple filters). Whole data is in excel table so I can apply Excel's build-in table filters to display exact data that I need.
However it is always difficult to apply multiple filter to display expected data, specially as I need to do it very frequently. To make things simple I'm planning to number each record like
a.b.c.d.e.f
Where a, b, c, d, e, f are simple numbers. List looks like:
1
1.1
1.2
1.2.1
1.2.1.1
1.2.2
1.3
& so on.
Problem is, Excel take it as number with single decimal but as soon as I add second decimal, excel treat it as text, which is obvious in general behavior.
However, as special case, I need excel treat both as number or text. Number is preferable as I want to sort them, which might be difficult as a text.
To make the things little more complex, while filtering in table, I require if I can add some formula to filter results like 1.* should display all numbers starts with 1.
Is it possible with excel's default behavior, without VBA?
If no, is it possible with VBA? If yes, any clue is appreciated. I don't need whole program as I can write basic VBA program, just a clue how it can be done?
I sort mine by adding a helper column that adds a letter to the front and sort on that. E.g. 1 becomes f1, 1.1 becomes f1.1 etc. Then all are sorted as text.
You can use the formula ="f" & A1.
My sample:
Then the data sorted:
And the filter:
If I were to try this without VBA, my first step would be to use the sort to columns function on the data tab.
Next make sure all empty spaces in your data are filled with zeros.
Then sort the data by column
as long as you left your original data in the same row as the sorted data (I didn't in the images posted to focus on the process), your items should now be in order.

Resources