Parenthesis/Brackets Matching and Maximum Elements using Stack algorithm - data-structures

Question: Suppose we are looking to process this expression ( [ ( { [ ] [ ] } ( ( ( ) ) ) ) { } ] ) using a stack. What is the maximum number of elements on the stack at any one time?
I understand that stack runs in LIFO order, so I am not sure how to match that with maximum number of elements and how does that relate to the expression anyway. The question is unclear for me. What do you think please?
I tried to answer it and got 9, but I found that maximum number of elements on the stack at any one time is 6 based on answer I got.

If you got an opening bracket, you put it on the stack. If you got a closing bracket which matches the opening bracket on the top of the stack, you pop this bracket from stack, else you put in on the stack. In this expression the stack would look like this:
(
( [
( [ (
( [ ( {
( [ ( { [
( [ ( {
( [ ( { [
( [ ( {
( [ (
( [ ( (
( [ ( ( (
( [ ( ( ( (
( [ ( ( (
( [ ( (
( [ (
( [
( [ {

Related

Problem in using filter context in CALCULATE

In PowerPivot DataModel I have written following DAX Code:
=VAR var1 =
CALCULATE (
SUMX ( 'CapitalPrepayment', 'CapitalPrepayment'[Amount] ),
'CapitalPrepayment'[Subject] = "Proforma",
ALL ( 'CapitalPrepayment'[EffectiveYear] )
)
VAR var2 =
CALCULATE (
SUMX ( 'CapitalPrepayment', 'CapitalPrepayment'[Amount] ),
'CapitalPrepayment'[EffectiveYear] = "1399",
'CapitalPrepayment'[Subject] = "Invoice"
)
VAR var3 =
CALCULATE (
SUMX ( 'CapitalPrepayment', 'CapitalPrepayment'[Amount] ),
'CapitalPrepayment'[Subject] = "Proforma",
'CapitalPrepayment'[EffectiveYear] = "1398"
)
VAR var5 =
CALCULATE (
SUMX ( 'CapitalPrepayment', 'CapitalPrepayment'[Amount] ),
ALL ( 'CapitalPrepayment'[Subject] )
)
RETURN
IF ( Var5 < 0, 0, IF ( Var5 = 0, 0, IF ( VAR1 + VAR2 <= 0, Var3, 0 ) ) )
and the result is:
As you can see, the problem is that grand total of [Var5] for some vendors and grand total for [Final] for all vendors is highly related to filters come from EffectiveYear ( at least Ithink so ), so the result would be zero. in addition, changing the filter context from visual ( for example removing [vendor] and [EffectiveYear] ) will disrupt the result. how can I fix this?
In my written DAX, [Var1]~[Var3] do include [subject] and [EffectiveYear] as filter arguments, but [Var5] does not for [EffectiveYear] . so I expect that the problem arise from [EffectiveYear].
I tried to include [EfectiveYear] in someway in CALCULATE but it didn't work.

Count unique matching items with filter as a calculated column

I have two tables are Data and Report.
Data Table:
In Data table contain three columns are Item, status, and filter.
The item contains duplicated entry and the item column contains text and number or number only or text only.
The status column contains two different text/comments, "Okay" and "Not Okay"
The filter column contains two different filters which are A1 and A2.
The report table
In the Report table, I updated both comments/text as "Okay" or "Not Okay". I am looking for count against filter A1 and A2 according to the comments.
I would like to create a new calculated column in the report table in order to get the unique count according to the comments and filter based on the data table columns item and status.
DATA:
REPORT
Alexis Olson helped the following calculated column in order to get the unique count. I am trying to add one more filter in existing DAX calculated column but it's not working. Can you please advise?
1.Desired Result =
VAR Comment = REPORT[COMMENTS]
RETURN
CALCULATE (
DISTINCTCOUNT ( DATA[ITEM] ),
DATA[STATUS] = Comment
)
2.Desired Result =
COUNTROWS (
SUMMARIZE (
FILTER ( DATA, DATA[STATUS] = REPORT[COMMENTS] ),
DATA[ITEM]
)
)
3.Desired Result =
SUMX (
DISTINCT ( DATA[ITEM] ),
IF ( CALCULATE ( SELECTEDVALUE ( DATA[STATUS] ) ) = REPORT[COMMENTS], 1, 0 )
)
I think you can just add a filter to CALCULATE:
Filter by A1 Result =
VAR Comment = REPORT[COMMENTS]
RETURN
CALCULATE (
DISTINCTCOUNT ( DATA[ITEM] ),
DATA[STATUS] = Comment,
DATA[FILTER] = "A1"
)
For the second method,
Filter by A1 Result =
COUNTROWS (
SUMMARIZE (
FILTER ( DATA, DATA[STATUS] = REPORT[COMMENTS] && REPORT[FILTER] = "A1" ),
DATA[ITEM]
)
)
I do not recommend using the third one but it would be like this
Filter by A1 Result =
SUMX (
DISTINCT ( DATA[ITEM] ),
IF (
CALCULATE ( SELECTEDVALUE ( DATA[STATUS] ) ) = REPORT[COMMENTS]
&& CALCULATE ( SELECTEDVALUE ( DATA[FILTER] ) ) = "A1",
1,
0
)
)

Function 'SAMEPERIODLASTYEAR' expects a contiguous selection Issue

I have a tabular data model in Visual Studio, I made a formula to get last year's sales over a given period:
Selected Measure LY:= CALCULATE([Selected Measure], SAMEPERIODLASTYEAR('date' [date]))
I made a formula to have the decomposition of all the months in the 'date' table:
FY_Month='date' [FY Month No]&'date' [Month_label]
And:
FY_Month_No=format(if('date'[month_no]>6,'date'[month_no]-6,'date'[month_no]+6), "00")
Month_label : all the months of the year
The "Selected Measure LY" display works fine when I have all months selected but when I remove one I get this error message:
Calculation error in "Selected Measure LY": the "SAMEPERIODLASTYEAR" function expects a contiguous selection when the date column comes from a tabkle on side 1 of a bidirectional relationship.
Between my 'date' table and my sales table I have a 1/N relationship, I tried to change this relationship too but the error always comes back.
I looked for solutions on forums and I found this but I don't find the same numbers as with my first formula:
Selected Measure LY:=
CALCULATE([Selected Measure],
FILTER (
ALL ( 'date' ),
YEAR ( 'date'[date] ) = YEAR ( TODAY () )
&& 'date'[date] <= TODAY ()
)
)
My code for Selected Measure:
Selected Measure:=
VAR hasFilter =
HASONEFILTER ( 'Measure Selection'[Measure Name] )
VAR selMeas =
SELECTEDVALUE ( 'Measure Selection'[Measure Name], BLANK () ) /*SWITCH works slow, so using many IFs*/
VAR preCalc =
IF (
selMeas = "Units",
[Sku Piece Quantity],
IF (
selMeas = "Net Sales",
[Euro Net Amount],
IF (
selMeas = "Gross Sales",
[Euro Gross Amount],
IF (
selMeas = "Average Wholesale",
[Average_Wholesale],
IF (
selMeas = "Average Units",
[Average Units],
IF (
selMeas = "No of Styles",
[Style Count],
IF (
selMeas = "No of Colours",
[Colour Count],
IF (
selMeas = "ROD by Style",
[ROD by Style],
IF (
selMeas = "Standard Cost",
[Sum_Cost_Base],
IF (
selMeas = "Net Margin",
[Net_Margin],
IF (
selMeas = "Gross Margin",
[Gross_Margin],
IF ( selMeas = "ROD by Colour", [ROD by Colour], BLANK () )
)
)
)
)
)
)
)
)
)
)
)
VAR calc =
IF ( hasFilter, preCalc, BLANK () )
RETURN
calc
How can I correct it?

SQL ERROR When running query

I've been trying to execute below code, but it's not running. SQLFiddle gives
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.'EO' AS 'EO','EF'.'AC' AS 'AC','EO'' at line 2
I don't know what I'm doing wrong.
SELECT DISTINCT
'EO'.'EO' AS 'EO',
'EF'.'AC' AS 'AC',
'EO'.'WO' AS 'WO',
'EO'.'WOF' AS 'WOF',
'EF'.'PN_SN' AS 'PN_SN',
'EC'.'CD' AS 'CD',
'EF'.'PN' AS 'PN',
'EO'.'EOC' AS 'EOC',
'EO'.'STAT' AS 'STAT',
'EF'.'OVRD' AS 'OVRD',
'EF'.'SEL' AS 'SEL',
'EF'.'ED' AS 'ED',
'EF'.'SCHED' AS 'SCHED',
'EF'.'SAD' AS 'SAD',
'EF'.'SA' AS 'SA',
'EO'.'EOD' AS 'EOD'
FROM
EO,
EC,
EF
WHERE
(
'EO'.'EO' = 'EC'.'EO'
)
AND
(
'EF'.'PSN' = 'EC'.'PSN'
)
AND
(
'EO'.'WB' = 'Y'
)
AND
(
'EC'.'CONT' = 'W & B'
)
;
SELECT
'EC'.'RSTD' AS 'RSTD'
FROM EC RIGHT JOIN EF
ON ( 'EF'.'EO' = 'EC'.'EO' )
ON ( 'EF'.'AC' = 'EC'.'AC' )
ON ( 'EF'.'PN' = 'EC'.'PN' )
ON ( 'EF'.'PSN' = 'EC'.'PSN' )
Single quotes - ' - are for literals. Use double-quotes - " - for identifiers ...
SELECT
"EC"."RSTD"
FROM EC RIGHT JOIN EF
ON ( "EF"."EO" = "EC"."EO" )
ON ( "EF"."AC" = "EC"."AC" )
ON ( "EF"."PN" = "EC"."PN" )
ON ( "EF"."PSN' = "EC"."PSN" )
... or, even better, omit them altogether:
SELECT
EC.RSTD
FROM EC RIGHT JOIN EF
ON ( EF.EO = EC.EO )
ON ( EF.AC = EC.AC )
ON ( EF.PN = EC.PN )
ON ( EF.PSN = EC.PSN )
Obviously keep the single quotes when appropriate e.g. comparing a column to a literal:
where EO.WB = 'Y'
Also note that it is unnecessary to use a column alias when you're not changing the column name: select ec.rstd as rstd is redundant, and indeed annoying. Reserve their use for renaming columns e.g. select ec.rstd as my_new_col_name

Sorting all the columns in MDX queries

Below is my MDX query. Here I have applied sorting for one column, how can I sort all the columns, the reason behind this is, I no column should have any values like (null), null, empty, unknown etc.
WITH MEMBER PageSize AS
1
MEMBER [PageNumber] AS
1
MEMBER [Measures].[Orderby Measure] AS
[Measures].[Budget Delivered COGS]
MEMBER [Orderby] AS
"BASC"
SET ROWAXISWOF AS
NonEmpty (
( [Time Periods].[Fiscal Year].[Fiscal Year].Members, [Time Periods].[Fiscal Quarter].[Fiscal Quarter].Members, [Time Periods].[Fiscal Month].[Fiscal Month].Members, [Time Periods].[Fiscal Week].[Fiscal Week].Members ),
{
{ [Measures].[Budget Delivered COGS] },
{ [Measures].[Break Even Delivered] }
}
)
SET ROWAXISWF AS
Filter ( ROWAXISWOF, [Measures].[Budget Delivered COGS] )
SET ROWAXIS AS
IIf ( 'Filter' = "NotFilter", ROWAXISWF, ROWAXISWOF )
MEMBER [Measures].[MaxRowCount] AS
ROWAXIS .Count
SET ROWAXIS_Count AS
IIf (
'Paging' = "Paging",
(
CASE
WHEN [Orderby] = 'BASC'
THEN Tail (
TopCount ( ROWAXIS, PageSize * PageNumber, [Measures].[Orderby Measure] ),
PageSize
)
ELSE Tail (
BottomCount ( ROWAXIS, PageSize * PageNumber, [Measures].[Orderby Measure] ),
PageSize
) END ),
ROWAXISWOF
)
SELECT ( IIf (
'Paging' = "Paging",
(
{ [Measures].[Budget Delivered COGS], [Measures].[Break Even Delivered], [Measures].[MaxRowCount] } ),
{ [Measures].[Budget Delivered COGS], [Measures].[Break Even Delivered] }
) ) ON COLUMNS,
ROWAXIS_Count ON ROWS
FROM (
SELECT (
{ [Time Periods].[Fiscal Year].&[2011], [Time Periods].[Fiscal Year].&[2012], [Time Periods].[Fiscal Year].&[2013], [Time Periods].[Fiscal Year].&[2014], [Time Periods].[Fiscal Year].&[2015] } ) ON COLUMNS
FROM [Homestore Sales]
)
It will return the output as preceding.
In that query for the Break Even Delivered header it is showing (null) as the first value . How can we make the value which are not null to be on top? if that is for one column we can just apply a sorting. My requirement is, no column should have null values in the first cell if any of the cell contains values. Please suggest me any idea. Thanks in advance.
I have done it by myself. I have changed the ROWAXISWOF as follows.
SET ROWAXISWOF AS
NonEmpty (
NonEmpty (
(
[Time Periods].[Fiscal Year].[Fiscal Year].Members, [Time Periods].[Fiscal Quarter].[Fiscal Quarter].Members, [Time Periods].[Fiscal Month].[Fiscal Month].Members, [Time Periods].[Fiscal Week].[Fiscal Week].Members
),
{ [Measures].[Budget Delivered COGS] }),
{ [Measures].[Break Even Delivered] } )
I have included NonEmpty conditions for each measures. Hope this will help some one.

Resources