filter a list of tuples using filter() function and lambda expression - python-3.x

I am learning Python programming;i'm trying to filter a list of tuples using filter() function and lambda expression. but there is something wrong.
The code I have tried is as follows:
state_and_population = [
( 'Uttar Pradesh' , 199812341 ),
( 'Maharashtra' , 112372972 ),
( 'Bihar' , 103804637 ),
( 'West Bengal' , 91347736 ),
( 'Madhya Pradesh' , 72597565 ),
( 'Tamil Nadu' , 72138958 ),
( 'Rajasthan' , 68621012 ),
( 'Karnataka' , 61130704 ),
( 'Gujarat' , 60383628 ),
( 'Andhra Pradesh' , 49386799 )
]
filtered_object = filter( lambda s_a_p: [ s_a_p[0] , s_a_p[1] < 0 ] ,state_and_population )
print( list( filtered_object ) )
The list should print an empty list according to my given condition but it is showing all the elements
[('Uttar Pradesh', 199812341), ('Maharashtra', 112372972), ('Bihar', 103804637), ('West Bengal', 91347736), ('Madhya Pradesh', 72597565), ('Tamil Nadu', 72138958), ('Rajasthan', 68621012), ('Karnataka', 61130704), ('Gujarat', 60383628), ('Andhra Pradesh', 49386799)]

Your lambda lambda s_a_p: [ s_a_p[0] , s_a_p[1] < 0 ] returns a non-empty list which, in Python, is considered a True boolean that, in turn, causes filter to accept the corresponding item from the iterator.
Write the lambda as follows: lambda e: e[1] < 0

Related

How to SortByColumns, Search, Filter in powerapps

I got it to search for one column, but how to I add more items? ie "Material", "Destination",etc
SortByColumns(
Search(
Filter(
'Transfer Request',
"Not-Completed" in Putaway.Value
),
Search_SC.Text,
"Material"
),
"ID",
If(
SortDescending1,
Ascending,
Descending
)
)
The Search function can search over multiple columns:
SortByColumns(
Search(
Filter( 'Transfer Request', "Not-Completed" in Putaway.Value ),
Search_SC.Text,
"Material", "Destination", "OtherColumn" ),
"ID",
If( SortDescending1, SortOrder.Ascending, SortOrder.Descending ) )

DAX percentiles of sums

I have a table with the following fields:
[Date]
[Ref Nr]
[Units]
I'd like to do a SUM over [Units] for each value in [Date] and [Ref Nr] and then take a 80 percentile for each value in [Ref Nr].
I've tried the following but it doesn't work...
DEFINE
MEASURE 'Table'[Pctl] =
CALCULATE(
PERCENTILEX.INC(
'Table',
CALCULATE(
SUM('Table'[Units]),
ALLEXCEPT('Table',
'Table'[Date],
'Table'[Ref Nr]
)
),
0.8
),
ALLEXCEPT('Table',
'Table'[Ref Nr]
)
)
EVALUATE
FILTER(
SUMMARIZE(
'Table',
'Table'[Ref Nr],
"Percentile 80",
'Table'[Pctl]
),
'Table'[Pctl] <> 0
)
Could you please guide me how to make it work?
Thanks in advance :)
I think your Pct1 calculation should look as follows:
MEASURE 'Table'[Pctl] =
CALCULATE(
PERCENTILEX.INC(
VALUES('Table'[Date],
CALCULATE(SUM('Table'[Units])),
0.8
)
)
as it will be evaluated in the context of [Ref Nr] in the final query, VALUES('Table'[Date]) will only return dates for the current [Ref Nr], the rest of the calculation should be clear I think

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.

Using Case to match strings in sql server?

I am trying to use CASE in a SQL Select statement that will allow me to get results where I can utilize the length of one string to produce the resutls of another string. These are for non-matched records from two data sets that share a common ID, but variant Data Source.
Case statement is below:
Select Column1, Column2,
Case
When Column1 = 'Something" and Len(Column2) = '35' Then Column1 = "Something Else" and substring(Column2, 1, 35)
End as Column3
From dbo.xxx
When I run it I get the following error:
Msg 102, Level 15, State 1, Line 5 Incorrect syntax near '='.
You need to have a value for each WHEN, and ought to have an ELSE:
Select Data_Source, CustomerID,
CASE
WHEN Data_Source = 'Test1' and Len(CustomerName) = 35 THEN 'First Value'
WHEN Data_Source = 'Test2' THEN substring(CustomerName, 1, 35)
ELSE 'Sorry, no match.'
END AS CustomerName
From dbo.xx
FYI: Len() doesn't return a string.
EDIT:
A SQL Server answer that addresses some of the comments might be:
declare #DataSource as Table ( Id Int Identity, CustomerName VarChar(64) )
declare #VariantDataSource as Table ( Id Int Identity, CostumerName VarChar(64) )
insert into #DataSource ( CustomerName ) values ( 'Alice B.' ), ( 'Bob C.' ), ( 'Charles D.' )
insert into #VariantDataSource ( CostumerName ) values ( 'Blush' ), ( 'Dye' ), ( 'Pancake Base' )
select *,
-- Output the CostumerName padded or trimmed to the same length as CustomerName. NULLs are not handled gracefully.
Substring( CostumerName + Replicate( '.', Len( CustomerName ) ), 1, Len( CustomerName ) ) as Clustermere,
-- Output the CostumerName padded or trimmed to the same length as CustomerName. NULLs in CustomerName are explicitly handled.
case
when CustomerName is NULL then ''
when Len( CustomerName ) > Len( CostumerName ) then Substring( CostumerName, 1, Len( CustomerName ) )
else Substring( CostumerName + Replicate( '.', Len( CustomerName ) ), 1, Len( CustomerName ) )
end as 'Crustymore'
from #DataSource as DS inner join
#VariantDataSource as VDS on VDS.Id = DS.Id
Select
Column1,
Column2,
Case
When Column1 = 'Something' and Len(Column2) = 35
Then 'Something Else' + substring(Column2, 1, 35)
End as Column3
From dbo.xxx
Update your query on
use '+' for string concat
len() returns int, no need to use ''
remove "Column1 =" in the case when condition
replace "" with ''
Hope this help.

Resources