Excel Pivot Table_Same column and row item with more than one values - excel

I have a table like this
I hope I can get the latest value when a product has more than one value in a same measurement item by excel pivot table. Like this:
I tried so hard but still have no way to achieve this...
Any idea? Thanks.

You'll need to add your source data to the Data Model (via the Power Pivot ribbon), after which you can create the following measures:
Weight
VAR LatestDate =
MAXX( 'Table', IF( 'Table'[measure item] = "weight", 'Table'[time] ) )
RETURN
MAXX(
'Table',
IF(
'Table'[measure item] = "weight",
IF( 'Table'[time] = LatestDate, 'Table'[value] )
)
)
Height
VAR LatestDate =
MAXX( 'Table', IF( 'Table'[measure item] = "height", 'Table'[time] ) )
RETURN
MAXX(
'Table',
IF(
'Table'[measure item] = "height",
IF( 'Table'[time] = LatestDate, 'Table'[value] )
)
)
Create a Pivot Table with these two measures in the Values field together with product name in the Rows field.

Related

MDX return caption as value

I got a following MDX query:
SELECT
NON EMPTY
[Measures].[Closed Events] ON COLUMNS
,NON EMPTY
Filter
(
{
[Date].[Year].&[2017]*
([Date].[Week Number].&[36] : [Date].[Week Number].&[52])*
[Visited Contact].[Contact SF Id].Children*
[Assignee].[Role Name].&[PL - Sales Rep HCP]
}
,
[Measures].[Closed Events] > 0
) ON ROWS
FROM [Visit Analysis];
How could I influence set within the filter so that I have both year and week number represented as numbers in excel (data table) and not strings?
What about measures?
With
Member [Measures].[Year] as
Cint([Date].[Year].CurrentMember.Name)
Member [Measures].[Weak] as
Cint([Date].[Week Number].CurrentMember.Name)
SELECT
NON EMPTY
{[Measures].[Closed Events],[Measure].[Year],[Measure].[Weak]} ON COLUMNS
,NON EMPTY
Filter
(
{
[Date].[Year].&[2017]*
([Date].[Week Number].&[36] : [Date].[Week Number].&[52])*
[Visited Contact].[Contact SF Id].Children*
[Assignee].[Role Name].&[PL - Sales Rep HCP]
}
,
[Measures].[Closed Events] > 0
) ON ROWS
FROM [Visit Analysis];

PowerPivot Filter Function

In PowerPivot Excel 2016 I write a formula to summarize year to date sales using filter function as below:
SalesYTD:=CALCULATE (
[Net Sales],
FILTER (
ALL ( Sales),
'sales'[Year] = MAX ( 'Sales'[Year] )
&& 'Sales'[Date] <= MAX ( 'Sales'[Date] )
)
)
And it's work perfectly, now in my data I have a field called "Channel" which I want to filter it in my pivot table but it won't works!
Does anybody knows how should I fix this formula?!
Thanks in advance...
Try:
SalesYTD:=CALCULATE (
[Net Sales],
FILTER (
ALLEXCEPT ( 'Sales', 'Sales'[Channel] ),
'sales'[Year] = MAX ( 'Sales'[Year] )
&& 'Sales'[Date] <= MAX ( 'Sales'[Date] )
)
)
ALLEXCEPT removes all context filters in the table except filters that have been applied to the specified columns, in this case [Channel] column.
Let me know if this helps.

Identify first occurence of event based on multiple criterias

I have a dataset in PowerPivot and need to find a way to flag ONLY the first occurrence of a customer sub event
Context: Each event (COLUMN A) can have X number of sub events (COLUMN B),
I already have a flag that identifies a customer event based on multiple criteria's (COLUMN D)... What I need is a way to flag only the first occurrence of a customer sub event within each event, I've added a fake COLUMN E to illustrate how the flagging should work.
UPDATE
Additional situation - Having duplicated customer sub_events but only need to flag the first sub_event... should look like this:
Create a calculated column in your model using the following expression:
=
IF (
[Customer_Event] = 1
&& [Sub_Event]
= CALCULATE (
FIRSTNONBLANK ( 'Table'[Sub_Event], 0 ),
FILTER (
'Table',
'Table'[Event] = EARLIER ( 'Table'[Event] )
&& [Customer_Event] = 1
)
),
1,
0
)
If Sub_Event column is a number replace FIRSTNONBLANK ( 'Table'[Sub_Event], 0 ) by MIN('Table'[Sub_Event])
Also if your machine regional settings use ; (semicolon) as list separator replace every , (comma) in my expression by a semicolon in order to match your settings.
UPDATE: Repeated values in Sub_Event column.
I think we can use CaseRow# column to get the first occurence of Sub_Event value:
=
IF (
[Customer_Event] = 1
&& [Sub_Event]
= CALCULATE (
FIRSTNONBLANK ( 'Table'[Sub_Event], 0 ),
FILTER (
'Table',
'Table'[Event] = EARLIER ( 'Table'[Event] )
&& [Customer_Event] = 1
)
)
&& [CaseRow#]
= CALCULATE (
MIN ( 'Table'[CaseRow#] ),
FILTER (
'Table',
'Table'[Event] = EARLIER ( 'Table'[Event] )
&& [Customer_Event] = 1
)
),
1,
0
)
It is not tested but should work.
Let me know if this helps.

SQL Pivot to Return more than 1 value

Here's the issue. I have 2 tables that I am currently using in a pivot to return a single value, MAX(Date). I have been asked to return additional values associated with that particular MAX(Date). I know I can do this with an OVER PARTITION but it would require me doing about 8 or 9 LEFT JOINS to get the desired output. I was hoping there is a way to get my existing PIVOT to return these values. More specifically, let's say each MAX(Date) has a data source and we want that particular source to become part of the output. Here is a simple sample of what I am talking about:
Create table #Email
(
pk_id int not null identity primary key,
email_address varchar(50),
optin_flag bit default(0),
unsub_flag bit default(0)
)
Create table #History
(
pk_id int not null identity primary key,
email_id int not null,
Status_Cd char(2),
Status_Ds varchar(20),
Source_Cd char(3),
Source_Desc varchar(20),
Source_Dttm datetime
)
Insert into #Email
Values
('test#test.com',1,0),
('blank#blank.com',1,1)
Insert into #History
values
(1,'OP','OPT-IN','WB','WEB','1/2/2015 09:32:00'),
(1,'OP','OPT-IN','WB','WEB','1/3/2015 10:15:00'),
(1,'OP','OPT-IN','WB','WEB','1/4/2015 8:02:00'),
(2,'OP','OPT-IN','WB','WEB','2/1/2015 07:22:00'),
(2,'US','UNSUBSCRIBE','EM','EMAIL','3/2/2015 09:32:00'),
(2,'US','UNSUBSCRIBE','ESP','SERVICE PROVIDER','3/2/2015 09:55:00'),
(2,'US','UNSUBSCRIBE','WB','WEB','3/2/2015 10:15:00')
;with dates as
(
select
email_id,
[OP] as [OptIn_Dttm],
[US] as [Unsub_Dttm]
from
(
select
email_id,
status_cd,
source_dttm
from #history
) as src
pivot (min(source_dttm) for status_cd in ([OP],[US])) as piv
)
select
e.pk_id as email_id,
e.email_address,
e.optin_flag,
/*WANT TO GET THE OPTIN SOURCE HERE*/ /*<-------------*/
d.OptIn_Dttm,
e.unsub_flag,
d.Unsub_Dttm
/*WANT TO GET THE UNSUB SOURCE HERE*/ /*<-------------*/
from #Email e
left join dates d on e.pk_id = d.email_id

EntitySQL 'IN subquery

I have the following T-SQL:
SELECT Cust.[CompanyName]
FROM Customers AS Cust
WHERE
(
Cust.[CompanyName] IN (SELECT CustSQ1.[CompanyName] AS [Customer Company name]
FROM Customers AS CustSQ1
WHERE
(
CustSQ1.Country = 'Argentina' )
) )
How do I do the same in EntitySQL?
I found the solution. Corresponding EntitySQL is:
"SELECT Cust.[CompanyName] FROM (SELECT CustSQ1.[CompanyName] FROM Customers AS CustSQ1 WHERE ( CustSQ1.Country = 'Argentina' )) AS Cust"

Resources