Lookup column with dates in PowerPivot - excel

I need to add a column on a table, computed from another one but I can't get the formula right.
Here are the tables and columns:
Table 1
Date (date)
Table 2
Key (text)
Start (date)
End (date)
I want to add a column to the Table 1 which will contains the Key from Table 2 where the Date column is between Start and End from Table 2.
If the date cannot be found in a range, then the field should be blank.

Try creating a calculated column in Table1 called Key and use this expression:
=
CALCULATE (
LASTNONBLANK ( Table2[Key], 0 ),
FILTER ( Table2, Table1[Date] >= Table2[Start] && Table1[Date] <= Table2[End] )
)
I am assuming in your table2 there is no overlaps between dates and
every date in Table1 will match only one start-end range.
It is not tested but should work, let me know if this works for you.

Related

PowerPivot Group By to find a count of values

I have a PowerPivot data model with thousands of rows and I'm trying to add a column that shows me the number of times a value occurs based on grouping the values in other columns. For example, I want to count the total number of times an item has shown $ greater than zero for a dept by for each combination of item and date. in the picture example provided here, 9 total stores show $ for the item for the specific date. I'm looking to do this in a column instead of a measure so I can merge it with other data. I have it solved as a measure with this formula:
CALCULATE (
SUMX (
ADDCOLUMNS (
SUMMARIZE (
Append1,
Append1[UPC],
Append1[Weeks]
),
"NumStoresSell", CALCULATE(
COUNT ( Append1[$] ),
Append1[$] <> 0
)
),
[NumStoresSell]
)
)
Any ideas on how I can solve it as a column?
thank you!
This DAX formula will return the number of rows in a table that matches all three criteria specified, row-by-row. The && means AND. You can swap this out with || for an OR criteria.
This example is a formula entered into a table named Summary and counts the rows in a table named Data with the columns for Item, Transaction Date and Transaction Amount having values matching the formula's row in the Summary Table.
= CALCULATE(
COUNTROWS(Data),
FILTER(Data,Data[Item]=Summary[Item],
&&Data[Transaction Date]=Summary[Transaction Date]
&&Data[Transaction Amount]=Summary[Transaction Amount]
)
)

Excel: Search within column and then search within the respective row

I have a table, let's call it Table 1, with a column identifier, ID, and for each ID (i.e. row) there are various respective dates.
In another sheet, there is a monthly calendar but also with a column having the ID.
I would like to have a function that would first read through the first column to determine which is the proper row from Table 1. After doing this it would check whether in this month there is any corresponding date from Table 1 for this particular ID (i.e. row).
Your help is much appreciated.
For this you can use the index,match,match way.
If you haven't used it yet,I will shortly explain.
The index function works as follows:
=index(A1:E10, 2, 5)
'Returns the value in the second row and the fifth column of the range "A1:E10" so the value of "E2"
The match function works as follows:
=match("Hello",A1:E1, 0)
'returns the index at which it found the value "Hello" e.g C1 has the value "Hello" it will return 3. The third paramter "0" says you want an EXACT match
Lets say your table is in range A1:D10 first row is header and first column IDS.
ID | Value1 | Value2 | Value 3
1 | ....
2 | ....
..
Now you want to find the entry at "Value1" with ID = 3
=index(A1:D10, match("Value1",B1:D1, 0), match(3, A1:A10, 0))

Spotfire: calculated column based on column in another table

Is it possible to insert a calculated column (table 1) based on another table (table 2)? Prefer to do it this way instead of joining table because the data on table 2 may keep on changing.
Calc column is derived by taking the f1 value on Table 2 based on matching value from col X on Table 1 to the nearest p1 value of Table 2. If it is possible to do a calculated column based on another table, how do I create an expression for it?
#p.ysl - In order to add column 'f1' to Table 1, Columns 'x' from Table 1 and 'p1' from Table 2 should be matched. As the format of these columns are not the same (one is real and the other is integer), we cannot match them. Though we do, 'f1' added to Table 1 will be blank as the values don't match.
You can add a calculated column to Table 1 with round values. example: 29.23 will be 29 in this column and then add column 'f1' from Table 2 by comparing calculated column 'round(x,0)' from Table 1 and column 'p1' from Table 2. But, the issue is calculated columns cannot be used for relating tables.
One solution is to freeze this calculated column in order to use it for matching columns. But, when we freeze the column, the entire table will be embedded and data cannot be refreshed.
However, you can accomplish this with an R-script.
Go to Register data function under Tools menu:
In the script section, add below script.
t3 <- cbind(t1, round(t1,0))
In Input Parameters section, define what 't1' is.
In Output parameters section, define what t3 is. In this case, the result will be stored as a table.
Now, RUN the script
It will prompt you to assign input and output parameters. Input - assign column 'x' from table 1.
Output parameter will be a new table.
Note: You can save this data function if you want to.
Table 't3' is created. Now, you can add 'f1' column from Table 2 to this table by matching 'column2' and 'p1' as shown in the screenshot below.
To ensure this runs dynamically, Table 1 and Table 2 can be embedded but t3 table should be linked to source so that when new data gets added to Table 1/ Table 2, t3 will be updated automatically.
Hope this helps!

excel pivot table - count appearance of certan value in column

let's say i have folowing data for my excel pivot table:
country_id user_id answer
---------- ------- ------
1 1 Y
1 2 Y
2 3 N
2 4 Y
3 5 N
i would like to count how many "Y" i have per country.
If i put "answer" in Values as "Count of answer" i get 1 for each row. How can i count only "Y" answers?
so the result will be:
country_id answerY
----------- -------
1 2
2 1
3 0
br
Y
I have a solution similar to #pkg. Add one more column in your data and call it "answerY" with the formula:
=IF(C4="Y",1,0)
or, if your data is in a table:
=IF([#answer]="Y",1,0)
Now, set up your pivot table as follows:
Row lables: country
Values: answerY (sum)
Ordinarily I'd say to add a calculated field in a pivot table, but calculated fields work off of the aggregate values, and I can't think of a way to do this with a straight pivot table.
If you have the ability to use PowerPivot, you could create a custom column or a Dax expression that would handle this.
I'm not sure you can do that in a pivot table, but if you would like to do it outside of a pivot table you could make a couple of columns with these formulas:
Column D:
=IF(C2="Y",1,0)*A2
Column E:
=COUNTIF(D$2:D$6,B2)
This assumes that all user IDs are unique and sequential, and D$6 needs to be replaced with whatever is the last value in the column. Column E will have the values you described as answerY.

Excel: Match\Index specific column based on multiple criteria

I have two Tables, Table 1 Column A is a rolling date column. Table 2 consists of four columns of differing "Trigger Dates". Table 2, Column 1 contains an Annual trigger, ie occurs once a year. Table 2 Column 2 contains Bi-Annual Triggers, and occurs twice yearly. Table 2 Column 3 contains Monthly Triggers, "12 dates" and Table 2 Column 4 contains Weekly Triggers, "52 dates".
Ok, so what I'm looking for is to a formula that will return the date trigger, where Table 1 Column 1 matches a condition (Annually, Bi-Annually, Monthly, Weekly) selected in a cell "we'll say F1, outside of any ranges that may be created.
Example: If Table 1 Col 2 = "01/02/2013" and I select "Weekly" in F1, the formula will look in the weekly Column in Table 2 to determine if "01/02/2013" exists.
I've allocated a named range to each column in Table 2
I've allocated a named range to the condition in F1 which is a drop down list
Any suggestions?
You might try:
=IFERROR(VLOOKUP(B2,INDIRECT(CHAR(72+MATCH(F$1,$I$1:$L$1,0))&":"&CHAR(72+MATCH(F$1,$I$1:$L$1,0))),1,0),"")
Ok, so supposing your Range Name is in F1, your formula to find the match would look as follows:
=If(IsError(Match(LookupVal,Indirect(F1),0)),"No Match", "Match")
...Something along those lines
Hope this helps point you in the right direction.
INDIRECT and VLOOKUP will be your friends here.
I constructed Table2 just as you had described, with the Annual,Bi-annual,Monthly,Weekly labels for the columns in the table - this will avoid having to keep named ranges up to date, as it's looking directly at the table in the formula
For the function, I then used VLOOKUP to find the date, in the column referenced by F1.
Column Reference: INDIRECT("Table2["&$F$1&"]")
Find Value: VLOOKUP([#Col2],INDIRECT("Table2["&$F$1&"]"),1,FALSE) (looking at column 1 in the return values, as I don't really care what value is returned.
This will return the date if found, and an error if not. I can then wrap the result in the IF(ISERROR( so I can return Yes or No if the date is found.
This produces the entire formula of
=IF(ISERROR(VLOOKUP([#Col2],INDIRECT("Table2["&$F$1&"]"),1,FALSE)),"No","Yes")
for the next column in Table1, so that when I change the value in F1, it will look for that column name in Table2, and let me know if it is found.

Resources