I have warning " singleton variable in branch B " - prolog

max ( ) :-
write ( ' number one ' ) , nl ,
read ( A ) ,
write ( ' number two ' ) , nl ,
read ( B ) ,
A > B , write ( A ) ; write ( B ) .
max ( ) :-
write ( ' number one ' ) , nl ,
read ( A ) ,
write ( ' number two ' ) , nl ,
read ( B ) ,
A > B , write ( A ) ; write ( B ) .

Related

find percentage per unique fields value in a long-format table in powerbi dax

I have the following snip of a table in power bi:
columns: ['id','fields','io','status','value']
I would like to calculate for each unique value of the column field per id with a condition that the status value is complete.
+----+---------+---------+-----+
| id | field | status | % |
--------------------------------
| 112| zip | complete| 60 |
| 112| gender | complete| 54 |
...
| 245| zip | complete| 85 |
+----+=--------+---------+-----+
the idea is (sum of complete) / (sum of complete+missing+error) per id.
I am not sure how to approach this with a long format table.
try this :
Completed % =
CALCULATE (
SUM ( 'Table (2)'[value] ),
ALLEXCEPT ( 'Table (2)', 'Table (2)'[id], 'Table (2)'[fields] ),
'Table (2)'[status] = "Complete"
)
/ CALCULATE (
SUM ( 'Table (2)'[value] ),
ALLEXCEPT ( 'Table (2)', 'Table (2)'[id] )
)
You can use this code as a measure:
Completed_Percent =
VAR SummaryTable =
ADDCOLUMNS (
FILTER (
SUMMARIZE ( Table1, Table1[id], Table1[fields], Table1[status] ),
Table1[status] = "complete"
),
"%",
DIVIDE (
100 * CALCULATE ( SUM ( Table1[value] ) ),
CALCULATE ( SUM ( Table1[value] ), ALL ( Table1 ) )
)
)
VAR Result =
SUMX ( SummaryTable, [%] )
RETURN
Result
Then Create a table or Matrix, Put [id], [field],[status] into row area, and put "Completed_Percent" measure in the value area. Done!
If we test it:

DAX/PowerBI Rank taking a variable/parameter into account

I'm working on a ranking/scoring system and I'm missing the PERCENTRANK.INC function in powerBI. Instead I have worked out below formula which is the closest I can get.
Score =
DIVIDE (
RANKX (
FILTER ( 'Table', NOT ( ISBLANK ( [Sold amounts] ) ) ),
[Sold amounts],
,
ASC
) - 1,
COUNTROWS ( FILTER ( 'Table', NOT ( ISBLANK ( [Sold amounts] ) ) ) ) - 1
)
I really want to have the formula to take the type of "Fruit" into account in my scoring/ranking.
In short each fruit should be scored separately, with a range per fruit sold.
Could this somehow be done with a variable (VAR)?
Example of data:
This should work.
Score =
VAR fruit = 'Table'[Fruit]
VAR filteredTable = FILTER ( 'Table', NOT ( ISBLANK ( [Sold amount] ) ) && 'Table'[Fruit] = fruit)
RETURN
DIVIDE (
RANKX (
filteredTable,
[Sold amount],
,
ASC
) - 1,
COUNTROWS ( filteredTable ) - 1
)

how to compute the growth rate in Power Bi using Dax?

I want to have a column in power bi showing the growth rate of sales. I have a table like
year
count
1395
123
1396
232
1397
23
1398
908
1399
678
1400
34
the growth rate is (this year - previous year)/previous year
could you please guide me how I can do this?
When I use the growth the data is like below
You can add a calculated column like this:
growth =
VAR _currentcount = 'Table'[count]
VAR _currentyear = 'Table'[year]
VAR _previouscount =
CALCULATE (
SELECTEDVALUE ( 'Table'[count] ) ,
ALL ( 'Table' ) ,
'Table'[year] = _currentyear - 1
)
RETURN
IF (
NOT ISBLANK ( _previouscount ) ,
DIVIDE ( _currentcount , _previouscount ) - 1
)
or a measure like this, to be used with your year dimension:
growth_measure =
VAR _currentcount = SELECTEDVALUE ( 'Table'[count] )
VAR _currentyear = SELECTEDVALUE ( 'Table'[year] )
VAR _previouscount =
CALCULATE (
SELECTEDVALUE ( 'Table'[count] ) ,
ALL ( 'Table' ) ,
'Table'[year] = _currentyear - 1
)
RETURN
IF (
NOT ISBLANK ( _previouscount ) ,
DIVIDE ( _currentcount , _previouscount ) - 1
)
Giving this result:
All depending on your needs.

Query works in PLSQL Developer but doesn't in SQL Developer

So, I have this query that's just making me go crazy. For some crazy reason, the query runs and brings the desired output on PLSQL Developer, but on SQL Developer it doesn't! As far as I know, the exact same query should work on both, not just one or the other. Not everybody at my team has PLSQL Dev, so it has to work on both. This query also shows different behaviour on my BI Application. Has anyone seen this crazy behaviour before?
Here's the query:
with t1 as (
SELECT
case when CLG_STATUS.NM_STATUS in ('1 - ATIVO','5 - BLOQUEADO','7 - AGUARDANDO AUTENTICAÇÃO') then 'LG Club Member'
else 'Non Member'
end membership,
Round(
Round(SUM(NVL(CASE WHEN CLG_VENDA.CD_TP_VENDA = 'D' THEN -1*CLG_VENDA.QTD_VENDA ELSE CLG_VENDA.QTD_VENDA END,0)), 1)
/
Count(distinct CLG_PARTICIPANTE.ID)
, 1) as average_sales ,
Round(
SUM(NVL(CASE WHEN CLG_VENDA.CD_TP_VENDA = 'D' THEN -1*CLG_VENDA.QTD_VENDA ELSE CLG_VENDA.QTD_VENDA END,0))
, 1) as sellout,
Count(distinct CLG_PARTICIPANTE.ID)
as "qty members"
FROM
CLG_VENDA_PONTO,
CLG_PARTICIPANTE,
CLG_STATUS,
CLG_VENDA,
CLG_CARGO CLG_CARGO_VENDA,
CLG_CANAL CLG_CANAL_VENDA,
CLG_REDE CLG_REDE_VENDA,
CLG_PRODUTO,
CLG_TP_MOVIMENTO,
DIM_PERIOD_DAY PERIOD_VENDA_DATA,
CLG_LOJA CLG_LOJA_VENDA
WHERE
( CLG_VENDA_PONTO.ID_VENDA=CLG_VENDA.ID(+) )
AND ( CLG_VENDA_PONTO.ID_PROD=CLG_PRODUTO.ID(+) )
AND ( CLG_VENDA_PONTO.ID_CARGO=CLG_CARGO_VENDA.ID )
AND ( CLG_LOJA_VENDA.ID=CLG_VENDA_PONTO.ID_LOJA )
AND ( CLG_REDE_VENDA.ID=CLG_LOJA_VENDA.ID_REDE )
AND ( CLG_CANAL_VENDA.ID=CLG_VENDA_PONTO.ID_CANAL )
AND ( CLG_PARTICIPANTE.ID_LOJA=CLG_LOJA_VENDA.ID )
AND ( CLG_LOJA_VENDA.ID_REDE=CLG_REDE_VENDA.ID )
AND ( CLG_PARTICIPANTE.ID=CLG_VENDA_PONTO.ID_PARTCPTE )
AND ( CLG_VENDA_PONTO.ID_TP_MOVIMENTO=CLG_TP_MOVIMENTO.ID )
AND ( CLG_STATUS.ID=CLG_PARTICIPANTE.ID_STATUS )
AND ( CLG_VENDA_PONTO.DATA_VENDA=PERIOD_VENDA_DATA.YYYYMMDD )
AND ( CLG_CARGO_VENDA.NM_CARGO = '5 - VENDEDOR' OR CLG_VENDA.ID_CARGO = 8 )
AND
(
CLG_PRODUTO.NM_PRODUTO NOT IN ( 'M4338 - FRETE','R39745 - FRETE SOBRE VENDAS' )
AND
CLG_TP_MOVIMENTO.NM_MOVIMENTO IN ( '1 - VENDA','3 - DEVOLUCAO' )
AND
CLG_REDE_VENDA.NM_REDE = '21540901 - RABELO'
AND
PERIOD_VENDA_DATA.YYYYMMDD_DATE BETWEEN to_date('01-01-2013', 'DD-MM-YYYY') AND to_date('31-12-2013', 'DD-MM-YYYY')
AND
CLG_CANAL_VENDA.NM_CANAL IN ('1 - VAREJO')
AND
CLG_PRODUTO.FG_MAPEADO IN ( 'SIM' )
)
GROUP BY
case when CLG_STATUS.NM_STATUS in ('1 - ATIVO','5 - BLOQUEADO','7 - AGUARDANDO AUTENTICAÇÃO') then 'LG Club Member'
else 'Non Member'
end
), t2 as (
SELECT
case when CLG_STATUS.NM_STATUS in ('1 - ATIVO','5 - BLOQUEADO','7 - AGUARDANDO AUTENTICAÇÃO') then 'LG Club Member'
else 'Non Member'
end membership,
Round(
Round(SUM(NVL(CASE WHEN CLG_VENDA.CD_TP_VENDA = 'D' THEN -1*CLG_VENDA.QTD_VENDA ELSE CLG_VENDA.QTD_VENDA END,0)), 1)
/
Count(distinct CLG_PARTICIPANTE.ID)
, 1) as average_sales ,
Round(
SUM(NVL(CASE WHEN CLG_VENDA.CD_TP_VENDA = 'D' THEN -1*CLG_VENDA.QTD_VENDA ELSE CLG_VENDA.QTD_VENDA END,0))
, 1) as sellout,
Count(distinct CLG_PARTICIPANTE.ID)
as "qty members"
FROM
CLG_VENDA_PONTO,
CLG_PARTICIPANTE,
CLG_STATUS,
CLG_VENDA,
CLG_CARGO CLG_CARGO_VENDA,
CLG_CANAL CLG_CANAL_VENDA,
CLG_REDE CLG_REDE_VENDA,
CLG_PRODUTO,
CLG_TP_MOVIMENTO,
DIM_PERIOD_DAY PERIOD_VENDA_DATA,
CLG_LOJA CLG_LOJA_VENDA
WHERE
( CLG_VENDA_PONTO.ID_VENDA=CLG_VENDA.ID(+) )
AND ( CLG_VENDA_PONTO.ID_PROD=CLG_PRODUTO.ID(+) )
AND ( CLG_VENDA_PONTO.ID_CARGO=CLG_CARGO_VENDA.ID )
AND ( CLG_LOJA_VENDA.ID=CLG_VENDA_PONTO.ID_LOJA )
AND ( CLG_REDE_VENDA.ID=CLG_LOJA_VENDA.ID_REDE )
AND ( CLG_CANAL_VENDA.ID=CLG_VENDA_PONTO.ID_CANAL )
AND ( CLG_PARTICIPANTE.ID_LOJA=CLG_LOJA_VENDA.ID )
AND ( CLG_LOJA_VENDA.ID_REDE=CLG_REDE_VENDA.ID )
AND ( CLG_PARTICIPANTE.ID=CLG_VENDA_PONTO.ID_PARTCPTE )
AND ( CLG_VENDA_PONTO.ID_TP_MOVIMENTO=CLG_TP_MOVIMENTO.ID )
AND ( CLG_STATUS.ID=CLG_PARTICIPANTE.ID_STATUS )
AND ( CLG_VENDA_PONTO.DATA_VENDA=PERIOD_VENDA_DATA.YYYYMMDD )
AND ( CLG_CARGO_VENDA.NM_CARGO = '5 - VENDEDOR' OR CLG_VENDA.ID_CARGO = 8 )
AND
(
CLG_PRODUTO.NM_PRODUTO NOT IN ( 'M4338 - FRETE','R39745 - FRETE SOBRE VENDAS' )
AND
CLG_TP_MOVIMENTO.NM_MOVIMENTO IN ( '1 - VENDA','3 - DEVOLUCAO' )
AND
CLG_REDE_VENDA.NM_REDE = '21540901 - RABELO'
AND
PERIOD_VENDA_DATA.YYYYMMDD_DATE BETWEEN add_months(to_date('01-01-2013', 'DD-MM-YYYY'), -12) AND add_months(to_date('31-12-2013', 'DD-MM-YYYY'), -12)
AND
CLG_CANAL_VENDA.NM_CANAL IN ('1 - VAREJO')
AND
CLG_PRODUTO.FG_MAPEADO IN ( 'SIM' )
)
GROUP BY
case when CLG_STATUS.NM_STATUS in ('1 - ATIVO','5 - BLOQUEADO','7 - AGUARDANDO AUTENTICAÇÃO') then 'LG Club Member'
else 'Non Member'
end
)
select t1.*, coalesce((t1.average_sales - t2.average_sales) / t2.average_sales, 0) as variation
from t1 join t2 on t1.membership = t2.membership
On SQL Developer it displays this:
ORA-00928: missing SELECT keyword
00928. 00000 - "missing SELECT keyword"
I'm totally at a loss here. How can a query parse at one tool and not the other ? I tried searching around the net but to no avail.
Thanks in advance!
It might be the line,
/
SQL Plus uses / by itself to mark the end of a sql query, and SQL Developer most likely follows. However, the preceding whitespace may change the meaning, so this may not be the problem.
To test this hypothesis move the / to the preceding line. Change
Round(SUM(NVL(CASE WHEN CLG_VENDA.CD_TP_VENDA = 'D' THEN -1*CLG_VENDA.QTD_VENDA ELSE CLG_VENDA.QTD_VENDA END,0)), 1)
/
Count(distinct CLG_PARTICIPANTE.ID)
To
Round(SUM(NVL(CASE WHEN CLG_VENDA.CD_TP_VENDA = 'D' THEN -1*CLG_VENDA.QTD_VENDA ELSE CLG_VENDA.QTD_VENDA END,0)), 1) /
Count(distinct CLG_PARTICIPANTE.ID)
If that is the problem, then it is a matter of reformatting to something that works for you that doesn't leave / on a line by itself.

Query Optimization/Performance

I have a sql query which is performing badly, It is causing table scans / IO spikes. Please check the below script and the execution plan and let me know if it requires any indexes or refactoring of the query itself. Thanks
--Query
SELECT TOP 20
CustomerPrimaryExtID,
Max(POSTimeStamp) AS TransactionDate,
ExtLocationCode,
0 AS RedemptionAmount,
0 AS RedemptionCount,
TerminalNum,
LogixTransNum,
POSTransNum AS TransNum,
0 AS DetailRecords,
CustomerTypeID,
PresentedCustomerID,
PresentedCardTypeID,
HHID,
Replayed,
0 AS TransContext,
ISNULL(TransTotal, 0) AS TransTotal
FROM TransHist AS TH WITH(nolock)
WHERE
(
(
( CustomerPrimaryExtID IN ( '' ) AND HHID IS NULL )
OR HHID = '0000000250000007320' AND CustomerTypeID <> 1
) OR ( CustomerPrimaryExtID = '0000000250000007320' AND CustomerTypeID = 1 )
)
AND NOT EXISTS
(
SELECT
LogixTransNum
FROM TransRedemptionView AS TR2
WHERE
(
( ( CustomerPrimaryExtID IN ( '' ) AND HHID IS NULL ) OR HHID = '0000000250000007320' AND CustomerTypeID <> 1 )
OR ( CustomerPrimaryExtID = '0000000250000007320' AND CustomerTypeID = 1 )
)
AND TH.LogixTransNum = TR2.LogixTransNum
)
GROUP BY
CustomerPrimaryExtID,
HHID,
CustomerTypeID,
PresentedCustomerID,
PresentedCardTypeID,
LogixTransNum,
POSTransNum,
TerminalNum,
ExtLocationCode,
Replayed,
TransTotal
ORDER BY TransactionDate DESC

Resources