For an Excel documenten I am fitlering the data to create a "view". I got several rows of data containing the following data
| type | sender | duration | price |
In my view I want the following columns:
| sender | duration | price |
Type = data / call
Sender = phone number (several different)
Duration = time in seconds
Price = is total price for seconds
In the view I want the unique list of phone numbers if type is data, then I want the total duration and total price. The latter of these is done using SUMIFS
I know that there's an option by filtering by hand. But I assume you already found that I want this in code.
I already tried XLOOKUP but this only returns one result as cell reference. XMATCH isn't the holy grail either.
I ended up using =UNIQUE(FILTER(...)) I'd hoped that XLOOKUP and XMATCH were there to help with this.
Feel free to feedback if you have a better formula, please!
Related
I have the following Excel table:
Date | Value
===========+=======
24.02.2019 | 1350
25.02.2019 | 1120
26.02.2019 | 1200
|
|
|
===========+=======
DIFFERENCE | 80
The bottom total row (DIFFERENCE) should calculate the difference between value of last inserted record in table and value of previous record. When I tomorrow insert the new record for 27.02.2019 with value 1300, the bottom row should show difference 100 (1300-1200). There are undefined number of blank rows between last inserted row and bottom total row.
And here is another example
=INDIRECT("B"&COUNTA(INDIRECT("B1:B"&ROW()-1)))-INDIRECT("B"&COUNTA(INDIRECT("B1:B"&ROW()-1))-1)
I think that OFFSET and INDIRECT are not necessary here, you can simply use INDEX like this:
=INDEX($B$2:$B$10,COUNTA($B$2:$B$10))-INDEX($B$2:$B$10,COUNTA($B$2:$B$10)-1)
Note: OFFSET and INDIRECT are volatile functions, while INDEX is not. Volatile means that if anything changes anywhere in your workbook, than its value has to be recalculated. In case of non-volatile function, however, recalculation is initiated only if its parameters (in this case $B$2:$B$10) change. So, in general my solution is more efficient than the other two posted before.
Well, this works, quick and dirty...
=OFFSET(B1,LOOKUP(2,1/(B:B<>""),ROW(B:B))-1,,)-OFFSET(B1,LOOKUP(2,1/(B:B<>""),ROW(B:B))-2,,)
If you want the result in the same column, then you will have to control the range B:B...
Suppose I have the following data in a spreadsheet, where the first row contains the column headings and the first column contains the row index (for reference):
||A | B | C |
==||================
0 ||2 | 3 | Y |
--||----------------
1 ||2 | 4 | Y |
--||----------------
2 ||3 | 5 | N |
--||----------------
3 ||8 | 3 | Y |
--||----------------
How can I get the sum of all the values in column B for which b - a >= 1 && c == "Y", in Google Sheets and Excel?
So essentially, the sum should factor in only rows 0 and 1 in which case the result should be 7.
I know this sounds like a very specific question but I did not know how else to describe it other than by example. The answer should be applicable in other similar scenarios.
Thanks for the help.
[Edit] In response to people voting down due to lack of research, well, I've tried to use the sumif() function but I got stuck immediately on the condition part as I am not sure how to compare the current item in the aggregation with another cell. I also tried to use the sumifs() function which allows for multiple criteria, but also to no avail. As for my research, I searched on Google but could not find anything, possibly due to my inability to express the requirement in a manner suitable for a google query. Therefore, I have presented the above as a way of explaining my requirement by example.
I hope this helps.
I appreciate that this may not be possible to do with the simple built in formulas. If this is the case, please mention it as that would also be useful to know.
Thank you.
Excel: =SUMPRODUCT(((B:B-A:A)>=1)*(C:C="Y")*(B:B))
Untested, but let me know if it works. Next time, remember to add an example of code/formulas that you have already tried and what error you ran into.
Edit:
Tested it, here is a screenshot of it working with your example data (disregard the fact that my Excel is in spanish)
This works by intersecting both logical tests (that is, performing a logical AND): (B-A)>=1 AND C="Y". Here you can see the result of each logical test and then, finally, where it evaluates to TRUE it returns the value in column B; where it's FALSE, it returns 0. Finally, it sums the values in the result array.
Say I have two tables. attrsTable:
file | attribute | value
------------------------
A | xdim | 5
A | ydim | 6
B | xdim | 7
B | ydim | 3
B | zdim | 2
C | xdim | 1
C | ydim | 7
sizeTable:
file | size
-----------
A | 17
B | 23
C | 34
I have these tables related via the 'file' field. I want a PowerPivot measure within attrsTable, whose calculation uses size. For example, let's say I want xdim+ydim/size for each of A, B, C. The calculations would be:
A: (5+6)/17
B: (7+3)/23
C: (1+7)/34
I want the measure to be generic enough so I can use slicers later on to slice by file or attribute. How do I accomplish this?
I tried:
dimPerSize := CALCULATE([value]/SUM(sizeTable[size])) # Calculates 0
dimPerSize := CALCULATE([value]/SUM(RELATED(sizeTable[size]))) # Produces an error
Any idea what I'm doing wrong? I'm probably missing some fundamental concepts here of how to use DAX with relationships.
Hi Redstreet,
taking a step back from your solution and the one proposed by Jacob, I think it might be useful to create another table that would aggregate all the calculations (especially given you probably have more than 2 tables with file-specific attributes).
So I have created one more table that contains (only) unique file names, and thus the relationships could be visualized this way:
It's much simpler to add necessary measures (no need for calculated columns). I have actually tested 2 scenarios:
1) create simple SUM measures for both Attribute Value and File Size. Then divide those two measures and job done :-).
2) use SUMX functions to have a bit more universal solution. Then the final formula for DimPerSize calculation could look like this:
=DIVIDE(
SUMX(DISTINCT(fileTable[file]),[Sum of AttrValue]),
SUMX(DISTINCT(fileTable[file]),[Sum of FileSize]),
BLANK()
)
With [Sum of AttrValue] being:
=SUM(attrsTable[value])
And Sum of FileSize being:
=SUM(sizeTable[size])
This worked perfectly fine, even though SUMX in both cases goes over all instances of given file name. So for file B it also calculates with zdim (if there is a need to filter this out, then use simple calculate / filter combination). In case of file size, I am using SUMX as well, even though it's not really needed since the table contains only 1 record for each file name. If there would be 2 instances, then use SUMX or AVERAGEX depending on the desired outcome.
This is the link to my source file in Excel (2010).
Hope this helps.
You look to have the concept of relationships OK but you aren't on the right track in terms of CALCULATE() either in terms of the structure or the fact that you can't simply use 'naked' numerical columns, they need to be packaged in some way.
Your desired approach is correct in that once you get a simple version of the thing running, you will be able to slice and dice it over any of your related dimensions.
Best practice is probably to build this up using several measures:
[xdim] = CALCULATE(SUM('attrstable'[value]), 'attrstable'[attribute] = "xdim")
[ydim] = CALCULATE(SUM('attrstable'[value]), 'attrstable'[attribute] = "ydim")
[dimPerSize] = ([xdim] + [ydim]) / VALUES('sizeTable'[size])
But depending on exactly how your pivot is set up, this is likely to also throw an error because it will try and use the whole 'size' column in your totals. There are two main strategies for dealing with this:
Use an 'iterative' formula such as SUX() or AVERAGEX() to iterate individually over the 'file' field and then adds up or averages for the total e.g.
[ItdimPerSize] = AVERAGEX(VALUES('sizeTable'[file]), [dimPerSize])
Depending on the maths you want to use, you might find that produce a useful average that you need to use SUMX but devide by the number of cases i.e. COUNTROWS('sizeTable'[file]).
You might decide that the totals are irrelevant and simply introduce an error handling element that will make them blank e.g.
[NtdimPerSize] = IF(HASONEVALUE('sizeTable'[file]),[dimPerSize],BLANK())
NB, all of this assumes that when you are creating your pivot that you are 'dragging in' the file field from the 'sizetable'.
Program: Excel 2010
I have a large selection of data which I filter for various reasons, I have been able to use the following to count my sales when filtered, however I want to be able add a 2nd criteria to the mix.
New: count by value B11 and C12
(B11) = Store Name
(C12) = Product Name
=SUMPRODUCT(--($C$38:$C$1000=(B11)),SUBTOTAL(3,OFFSET($C$38,ROW($C$38:$C$1000)-ROW($C$38),0)))
I have tried variations of the following however I keep getting errors:
=SUMPRODUCT(--($C$38:$C$1000=(B11),SUBTOTAL(3,OFFSET($C$38,ROW($C$38:$C$1000)-ROW($C$38),0)),(--($C$38:$C$1000=(C12),SUBTOTAL(3,OFFSET($C$38,ROW($C$38:$C$1000)-ROW($C$38),0)))
|Prod |Store
---------------
|ABC |CDA
|DEF |XYZ
|GHI |TUV
|ABC |XYZ
Prod = ABC; Store = CDA; Result = 1 (not 2)
Please help :-)
I'm not very familiar with SUBTOTAL, but this seems to work fine:
=SUMPRODUCT(--($C$38:$C$1000=(B11)),--($B$38:$B$1000=(C12)),SUBTOTAL(3,OFFSET($C$38,ROW($C$38:$C$1000)-ROW($C$38),0)))
I just added --($B$38:$B$50=(C12)) neat the beginning between the two expressions in the SUMPRODUCT
Another option to simplify formulas is to introduce a helper column which indicates whether the row is visible or not, e.g. in Z38 copied down
=SUBTOTAL(3,B38)
Now for your count with 2 criteria you can use COUNTIFS like this
=COUNTIFS(C:C,B11,B:B,C12,Z:Z,1)
Is there a method whereby columns in Excel (2003, 2007 and/or 2010) can be automatically shown (un-hidden) when the column to the left contains data?
Something like this:
IF column to the left contains data
THEN show column
+-----+-----+
| C | C | //If column1 contains data
| O | O | //Then reveal/show (unhide) column2
| L | L |
| U | U |
| M | M |
| N | N |
| 1 | 2 |
+-----+-----+
I'm guessing that VB code is required but am unsure as to what this would be.
Further to this, is there a way to automatically show the column going by the date (first day of each month)? This is a little more complicated. For example:
FOR all dates
IF system date = year(month.day1) //If it is the first day of a new month
THEN show column(month) //Then show the corresponding column for that month
ENDIF
ENDFOR
i.e. IF system date = 01/09/2012
THEN show column(September)
Is this possible?
Thank you.
Correct, you need VBA to achieve that. Use the Worksheet_Change event which fires whenever something changes. Then, use one of the various methods to determine if a column is not empty (http://ewbi.blogs.com/develops/2006/03/determine_if_a_.html, or just google). Alternatively, if this is to slow because if fires almost all the time, you could use the Worksheet_Activate() event (an overview of all Excel events can be found here).
If your column 7 contains data, you can unhide column 8 using MyWorksheet.Columns("8:8").Hidden = False.
Your second problem can be solved in a similar way. In the Worksheet_Activate(), check if today is the first day in a month using Day(Date) = 1 (I guess it needs to take into account that the user may not be using Excel this day, so the code should be a little more complex) and show that column using MyWorksheet.Columns("12:12").Hidden = False (or whichever it is) for December 1st.
All this code assumes that the columns are already there, just hidden.