private void ExpressionEditor_SetText()
        {
            ExpressionEditor_SuspendTextChanged = true;

            var i = UI.ExpressionEditor.SelectionStart;

            UI.ExpressionEditor.Text     = GetText();
            UI.ExpressionEditor.ReadOnly = ExpressionEditor_Current == null || !Handler.PowerBIGovernance.AllowEditProperty(ExpressionEditor_Current.ObjectType, GetProperty());
            if (!string.IsNullOrEmpty(UI.ExpressionEditor.Text))
            {
                if (syntaxHighlightTimer.Enabled)
                {
                    syntaxHighlightTimer.Enabled = false;
                }
                syntaxHighlightTimer.Interval = 1;
                ExpressionEditor_SyntaxHighlight();
            }
            UI.ExpressionEditor.ClearUndo();
            UI.ExpressionEditor.SelectionStart = i;

            // Only show label bar for DAX objects, when editing their expression:
            if (_expressionEditor_Current is IDaxObject)
            {
                UI.CurrentMeasureLabel.Text    = (_expressionEditor_Current as IDaxObject).DaxObjectName + (CurrentDaxProperty == DAXProperty.Expression ? "" : "." + CurrentDaxProperty.ToString()) + " :=";
                UI.CurrentMeasureLabel.Visible = true;
            }
            else
            {
                UI.CurrentMeasureLabel.Visible = false;
            }

            ExpressionEditor_SuspendTextChanged = false;

            LastDaxProperty = CurrentDaxProperty;
        }
        public static void SetDAX(this IDaxDependantObject obj, DAXProperty property, string expression)
        {
            if (obj is TablePermission tp && property == DAXProperty.Expression)
            {
                tp.FilterExpression = expression;
                return;
            }
            if (obj is KPI)
            {
                if (property == DAXProperty.StatusExpression)
                {
                    (obj as KPI).StatusExpression = expression; return;
                }
                if (property == DAXProperty.TargetExpression)
                {
                    (obj as KPI).TargetExpression = expression; return;
                }
                if (property == DAXProperty.TrendExpression)
                {
                    (obj as KPI).TrendExpression = expression; return;
                }
            }

            if (obj is IExpressionObject && property == DAXProperty.Expression)
            {
                (obj as IExpressionObject).Expression = expression;
                return;
            }

            if (obj is Measure && property == DAXProperty.DetailRowsExpression)
            {
                (obj as Measure).DetailRowsExpression = expression;
                return;
            }
            if (obj is Table && property == DAXProperty.DefaultDetailRowsExpression)
            {
                (obj as Table).DefaultDetailRowsExpression = expression;
                return;
            }

            /*if (obj is Measure m && property == DAXProperty.FormatStringExpression)
             * {
             *  m.FormatStringExpression = expression;
             *  return;
             * }*/
            if (obj is CalculationItem ci)
            {
                if (property == DAXProperty.Expression)
                {
                    ci.Expression = expression; return;
                }
                if (property == DAXProperty.FormatStringExpression)
                {
                    ci.FormatStringExpression = expression; return;
                }
            }


            throw new ArgumentException(string.Format(Messages.InvalidExpressionProperty, obj.GetTypeName(), property), "property");
        }
        private void ExpressionEditor_SetText()
        {
            ExpressionEditor_SuspendTextChanged = true;

            var i = UI.ExpressionEditor.SelectionStart;

            UI.ExpressionEditor.Text = GetText();
            if (!string.IsNullOrEmpty(UI.ExpressionEditor.Text))
            {
                if (syntaxHighlightTimer.Enabled)
                {
                    syntaxHighlightTimer.Enabled = false;
                }
                syntaxHighlightTimer.Interval = 1;
                ExpressionEditor_SyntaxHighlight();
            }
            UI.ExpressionEditor.ClearUndo();
            UI.ExpressionEditor.SelectionStart = i;

            // Only show label bar for DAX objects, when editing their expression:
            if (_expressionEditor_Current is IDaxObject)
            {
                UI.CurrentMeasureLabel.Text    = (_expressionEditor_Current as IDaxObject).DaxObjectName + " :=";
                UI.CurrentMeasureLabel.Visible = true;
            }
            else
            {
                UI.CurrentMeasureLabel.Visible = false;
            }

            ExpressionEditor_SuspendTextChanged = false;

            LastDaxProperty = CurrentDaxProperty;
        }
        public static string GetDAX(this IDaxDependantObject obj, DAXProperty property)
        {
            if (obj is TablePermission tp && property == DAXProperty.Expression)
            {
                return(tp.FilterExpression);
            }
            if (obj is KPI)
            {
                if (property == DAXProperty.StatusExpression)
                {
                    return((obj as KPI).StatusExpression);
                }
                if (property == DAXProperty.TargetExpression)
                {
                    return((obj as KPI).TargetExpression);
                }
                if (property == DAXProperty.TrendExpression)
                {
                    return((obj as KPI).TrendExpression);
                }
            }

            if (obj is IExpressionObject && property == DAXProperty.Expression)
            {
                return((obj as IExpressionObject).Expression);
            }

            if (obj is CalculationItem ci)
            {
                if (property == DAXProperty.Expression)
                {
                    return(ci.Expression);
                }
                if (property == DAXProperty.FormatStringExpression)
                {
                    return(ci.FormatStringExpression);
                }
            }

            if (TabularModelHandler.Singleton.CompatibilityLevel >= 1400)
            {
                if (obj is Measure && property == DAXProperty.DetailRowsExpression)
                {
                    return((obj as Measure).DetailRowsExpression);
                }
                if (obj is Table && property == DAXProperty.DefaultDetailRowsExpression)
                {
                    return((obj as Table).DefaultDetailRowsExpression);
                }
            }

            /*if (obj is Measure m && property == DAXProperty.FormatStringExpression)
             *  return m.FormatStringExpression;*/

            throw new ArgumentException(string.Format(Messages.InvalidExpressionProperty, obj.GetTypeName(), property), "property");
        }
        public static string GetDAXProperty(this IDaxDependantObject obj, DAXProperty property)
        {
            if (obj is TablePermission tp && property == DAXProperty.Expression)
            {
                return(Properties.FILTEREXPRESSION);
            }
            if (obj is KPI)
            {
                if (property == DAXProperty.StatusExpression)
                {
                    return(Properties.STATUSEXPRESSION);
                }
                if (property == DAXProperty.TargetExpression)
                {
                    return(Properties.TARGETEXPRESSION);
                }
                if (property == DAXProperty.TrendExpression)
                {
                    return(Properties.TRENDEXPRESSION);
                }
            }

            if (obj is IExpressionObject && property == DAXProperty.Expression)
            {
                return(Properties.EXPRESSION);
            }

            if (obj is CalculationItem ci)
            {
                if (property == DAXProperty.Expression)
                {
                    return(Properties.EXPRESSION);
                }
                if (property == DAXProperty.FormatStringExpression)
                {
                    return(Properties.FORMATSTRINGEXPRESSION);
                }
            }

            if (TabularModelHandler.Singleton.CompatibilityLevel >= 1400)
            {
                if (obj is Measure && property == DAXProperty.DetailRowsExpression)
                {
                    return(Properties.DETAILROWSEXPRESSION);
                }
                if (obj is Table && property == DAXProperty.DefaultDetailRowsExpression)
                {
                    return(Properties.DEFAULTDETAILROWSEXPRESSION);
                }
            }

            /*if (obj is Measure m && property == DAXProperty.FormatStringExpression)
             *  return m.FormatStringExpression;*/

            throw new ArgumentException(string.Format(Messages.InvalidExpressionProperty, obj.GetTypeName(), property), "property");
        }
 public IDaxObject GetObjectAt(DAXProperty property, int charIndex)
 {
     foreach (var kvp in InternalDictionary)
     {
         if (kvp.Value.Any(r => r.property == property && charIndex >= r.from && charIndex <= r.to))
         {
             return(kvp.Key);
         }
     }
     return(null);
 }
Exemple #7
0
 private void SetText(string value, DAXProperty prop)
 {
     if (ExpressionEditor_Current is IDaxDependantObject)
     {
         (ExpressionEditor_Current as IDaxDependantObject).SetDAX(prop, value);
     }
     else if (ExpressionEditor_Current != null)
     {
         ExpressionEditor_Current.Expression = value;
     }
 }
        internal void Add(IDaxObject dependsOn, DAXProperty property, int fromChar, int toChar, bool fullyQualified)
        {
            var dep = new ObjectReference {
                property = property, from = fromChar, to = toChar, fullyQualified = fullyQualified
            };
            List <ObjectReference> depList;

            if (!InternalDictionary.TryGetValue(dependsOn, out depList))
            {
                depList = new List <ObjectReference>();
                InternalDictionary.Add(dependsOn, depList);
                InternalList.Add(dependsOn);
            }
            depList.Add(dep);
        }
        private void SetText(string value, DAXProperty prop)
        {
            // Do semicolon replacement if needed:
            if (ExpressionEditor_IsDax && Preferences.Current.UseSemicolonsAsSeparators)
            {
                value = ExpressionParser.SemicolonsToCommas(value);
            }

            if (ExpressionEditor_Current is IDaxDependantObject)
            {
                (ExpressionEditor_Current as IDaxDependantObject).SetDAX(prop, value);
            }
            else if (ExpressionEditor_Current != null)
            {
                ExpressionEditor_Current.Expression = value;
            }
        }
Exemple #10
0
        public static string GetDescription(this DAXProperty property)
        {
            switch (property)
            {
            case DAXProperty.DetailRowsExpression: return("Detail Rows Expression");

            case DAXProperty.DefaultDetailRowsExpression: return("Default Detail Rows Expression");

            case DAXProperty.TargetExpression: return("KPI Target Expression");

            case DAXProperty.StatusExpression: return("KPI Status Expression");

            case DAXProperty.TrendExpression: return("KPI Trend Expression");

            case DAXProperty.FormatStringExpression: return("Format String Expression");

            default: return("Expression");
            }
        }
        public static void SetDAX(this IDaxDependantObject obj, DAXProperty property, string expression)
        {
            if (obj is RLSFilterExpression && property == DAXProperty.Expression)
            {
                var rls = (obj as RLSFilterExpression);
                rls.Role.RowLevelSecurity[rls.Table] = expression;
                return;
            }
            if (obj is KPI)
            {
                if (property == DAXProperty.StatusExpression)
                {
                    (obj as KPI).StatusExpression = expression; return;
                }
                if (property == DAXProperty.TargetExpression)
                {
                    (obj as KPI).TargetExpression = expression; return;
                }
                if (property == DAXProperty.TrendExpression)
                {
                    (obj as KPI).TrendExpression = expression; return;
                }
            }

            if (obj is IExpressionObject && property == DAXProperty.Expression)
            {
                (obj as IExpressionObject).Expression = expression;
                return;
            }

            if (obj is Measure && property == DAXProperty.DetailRowsExpression)
            {
                (obj as Measure).DetailRowsExpression = expression;
                return;
            }
            if (obj is Table && property == DAXProperty.DefaultDetailRowsExpression)
            {
                (obj as Table).DefaultDetailRowsExpression = expression;
                return;
            }

            throw new ArgumentException(string.Format(Messages.InvalidExpressionProperty, obj.GetTypeName(), property), "property");
        }
        public static string GetDAX(this IDaxDependantObject obj, DAXProperty property)
        {
            if (obj is RLSFilterExpression && property == DAXProperty.Expression)
            {
                var rls = (obj as RLSFilterExpression);
                return(rls.Role.RowLevelSecurity[rls.Table]);
            }
            if (obj is KPI)
            {
                if (property == DAXProperty.StatusExpression)
                {
                    return((obj as KPI).StatusExpression);
                }
                if (property == DAXProperty.TargetExpression)
                {
                    return((obj as KPI).TargetExpression);
                }
                if (property == DAXProperty.TrendExpression)
                {
                    return((obj as KPI).TrendExpression);
                }
            }

            if (obj is IExpressionObject && property == DAXProperty.Expression)
            {
                return((obj as IExpressionObject).Expression);
            }

            if (TabularModelHandler.Singleton.CompatibilityLevel >= 1400)
            {
                if (obj is Measure && property == DAXProperty.DetailRowsExpression)
                {
                    return((obj as Measure).DetailRowsExpression);
                }
                if (obj is Table && property == DAXProperty.DefaultDetailRowsExpression)
                {
                    return((obj as Table).DefaultDetailRowsExpression);
                }
            }

            throw new ArgumentException(string.Format(Messages.InvalidExpressionProperty, obj.GetTypeName(), property), "property");
        }
Exemple #13
0
        private void ExpressionEditor_Preview()
        {
            // TODO: Consider inspecting the expression of partitions, to determine
            // if the expression editor should use SQL syntax highlighting. This would
            // typically be the case for partitions using OLE DB or SQLNCLI providers.

            var obj = UI.TreeView.SelectedNode?.Tag as IExpressionObject;

            // Tables have "Default Detail Rows Expressions", but only for CompatibilityLevel 1400 or newer.
            if (obj is Table && !(obj is CalculatedTable) && Handler.CompatibilityLevel < 1400)
            {
                obj = null;
            }

            if (ExpressionEditor_IsEditing)
            {
                ExpressionEditor_AcceptEdit();
            }

            // Update the Expression Selector combobox:
            UI.ExpressionSelector.ComboBox.SelectedValueChanged -= ExpressionEditor_ExpressionSelectorChanged;
            UI.ExpressionSelector.Items.Clear();
            var ddo = obj as IDaxDependantObject;

            if (ddo != null)
            {
                UI.ExpressionSelector.Enabled = true;
                foreach (var daxProp in ddo.GetDAXProperties())
                {
                    UI.ExpressionSelector.Items.Add(DaxPropertyComboBoxItem.Items[daxProp]);
                }
                CurrentDaxProperty = ddo.GetDefaultDAXProperty();
            }
            else
            {
                UI.ExpressionSelector.Enabled = false;
            }
            UI.ExpressionSelector.ComboBox.SelectedValueChanged += ExpressionEditor_ExpressionSelectorChanged;

            ExpressionEditor_Current    = obj;
            UI.ExpressionEditor.Enabled = obj != null;
        }
        public static IList <DaxToken> Tokenize(this IDaxDependantObject obj, DAXProperty property)
        {
            var result = new List <DaxToken>();
            var dax    = obj.GetDAX(property);

            if (string.IsNullOrEmpty(dax))
            {
                return(result);
            }
            var lexer = new DAXLexer(new DAXCharStream(obj.GetDAX(property), false));

            lexer.RemoveErrorListeners();
            var lexerTokens = lexer.GetAllTokens();

            for (int i = 0; i < lexerTokens.Count; i++)
            {
                result.Add(new DaxToken(lexerTokens[i], result, i));
            }

            return(result);
        }
        private void ExpressionEditor_Preview(IExpressionObject obj)
        {
            // TODO: Consider inspecting the expression of partitions, to determine
            // if the expression editor should use SQL syntax highlighting. This would
            // typically be the case for partitions using OLE DB or SQLNCLI providers.

            if (ExpressionEditor_Current != null)
            {
                (IsNavigatingBack ? Forward : Back).Push(ExpressionEditor_Current);
            }

            if (ExpressionEditor_IsEditing)
            {
                ExpressionEditor_AcceptEdit();
            }

            // Update the Expression Selector combobox:
            UI.ExpressionSelector.ComboBox.SelectedValueChanged -= ExpressionEditor_ExpressionSelectorChanged;
            UI.ExpressionSelector.Items.Clear();
            var ddo = obj as IDaxDependantObject;

            if (ddo != null)
            {
                UI.ExpressionSelector.Enabled = true;
                foreach (var daxProp in ddo.GetDAXProperties())
                {
                    UI.ExpressionSelector.Items.Add(DaxPropertyComboBoxItem.Items[daxProp]);
                }
                CurrentDaxProperty = ddo.GetDefaultDAXProperty();
            }
            else
            {
                UI.ExpressionSelector.Enabled = false;
            }
            UI.ExpressionSelector.ComboBox.SelectedValueChanged += ExpressionEditor_ExpressionSelectorChanged;

            ExpressionEditor_Current    = obj;
            UI.ExpressionEditor.Enabled = obj != null;
        }
 public static DependsOnList GetDependencies(IDaxDependantObject obj, string dax, DAXProperty prop)
 {
     return(FormulaFixup.GetDependencies(obj, dax, prop));
 }
 static public void AddDep(this IDaxDependantObject target, IDaxObject dependsOn, DAXProperty property, int fromChar, int toChar, bool fullyQualified)
 {
     target.DependsOn.Add(dependsOn, property, fromChar, toChar, fullyQualified);
     if (!dependsOn.ReferencedBy.Contains(target))
     {
         dependsOn.ReferencedBy.Add(target);
     }
 }
 public DaxPropertyComboBoxItem(DAXProperty value)
 {
     Value = value; Description = Enum.GetName(typeof(DAXProperty), value).SplitCamelCase();
 }
        private static void ParseExpression(string dax, IDaxDependantObject expressionObj, DAXProperty prop, DependsOnList dependsOn = null)
        {
            var tokens = new DAXLexer(new AntlrInputStream(dax)).GetAllTokens();

            IToken lastTableRef    = null;
            int    startTableIndex = 0;

            for (var i = 0; i < tokens.Count; i++)
            {
                // TODO: This parsing could be used to check for invalid object references, for example to use in syntax highlighting or validation of expressions

                var tok = tokens[i];
                switch (tok.Type)
                {
                case DAXLexer.TABLE:
                case DAXLexer.TABLE_OR_VARIABLE:
                    if (i < tokens.Count - 1 && tokens[i + 1].Type == DAXLexer.COLUMN_OR_MEASURE)
                    {
                        // Keep the token reference, as the next token should be column (fully qualified).
                        lastTableRef    = tok;
                        startTableIndex = tok.StartIndex;
                    }
                    else
                    {
                        // Table referenced directly, don't save the reference for the next token.
                        lastTableRef = null;
                    }

                    if (Model.Tables.Contains(tok.Text.NoQ(true)))
                    {
                        if (dependsOn != null)
                        {
                            dependsOn.Add(Model.Tables[tok.Text.NoQ(true)], prop, tok.StartIndex, tok.StopIndex, true);
                        }
                        else
                        {
                            expressionObj.AddDep(Model.Tables[tok.Text.NoQ(true)], prop, tok.StartIndex, tok.StopIndex, true);
                        }
                    }
                    else
                    {
                        // Invalid reference (no table with that name) or possibly a variable or function ref
                    }
                    break;

                case DAXLexer.COLUMN_OR_MEASURE:
                    // Referencing a table just before the object reference
                    if (lastTableRef != null)
                    {
                        var tableName = lastTableRef.Text.NoQ(true);
                        lastTableRef = null;
                        if (!Model.Tables.Contains(tableName))
                        {
                            return;                                        // Invalid reference (no table with that name)
                        }
                        var table = Model.Tables[tableName];
                        // Referencing a column on a specific table
                        if (table.Columns.Contains(tok.Text.NoQ()))
                        {
                            if (dependsOn != null)
                            {
                                dependsOn.Add(table.Columns[tok.Text.NoQ()], prop, startTableIndex, tok.StopIndex, true);
                            }
                            else
                            {
                                expressionObj.AddDep(table.Columns[tok.Text.NoQ()], prop, startTableIndex, tok.StopIndex, true);
                            }
                        }
                        // Referencing a measure on a specific table
                        else if (table.Measures.Contains(tok.Text.NoQ()))
                        {
                            if (dependsOn != null)
                            {
                                dependsOn.Add(table.Measures[tok.Text.NoQ()], prop, startTableIndex, tok.StopIndex, true);
                            }
                            else
                            {
                                expressionObj.AddDep(table.Measures[tok.Text.NoQ()], prop, startTableIndex, tok.StopIndex, true);
                            }
                        }
                    }
                    // No table reference before the object reference
                    else
                    {
                        var table = (expressionObj as ITabularTableObject)?.Table;
                        // Referencing a column without specifying a table (assume column in same table):
                        if (table != null && table.Columns.Contains(tok.Text.NoQ()))
                        {
                            if (dependsOn != null)
                            {
                                dependsOn.Add(table.Columns[tok.Text.NoQ()], prop, tok.StartIndex, tok.StopIndex, false);
                            }
                            else
                            {
                                expressionObj.AddDep(table.Columns[tok.Text.NoQ()], prop, tok.StartIndex, tok.StopIndex, false);
                            }
                        }
                        // Referencing a measure or column without specifying a table
                        else
                        {
                            Measure m = null;
                            if (table != null && table.Measures.Contains(tok.Text.NoQ()))
                            {
                                m = table.Measures[tok.Text.NoQ()];
                            }
                            else
                            {
                                m = Model.Tables.FirstOrDefault(t => t.Measures.Contains(tok.Text.NoQ()))?.Measures[tok.Text.NoQ()];
                            }

                            if (m != null)
                            {
                                if (dependsOn != null)
                                {
                                    dependsOn.Add(m, prop, tok.StartIndex, tok.StopIndex, false);
                                }
                                else
                                {
                                    expressionObj.AddDep(m, prop, tok.StartIndex, tok.StopIndex, false);
                                }
                            }
                        }
                    }
                    break;

                default:
                    lastTableRef = null;
                    break;
                }
            }
        }
        /// <summary>
        /// This method can be called to obtain a DependsOnList for any DAX expression. This is useful when the
        /// user is currently editing the DAX expression of an object, but the expression has not been saved to
        /// the object.
        /// </summary>
        public static DependsOnList GetDependencies(IDaxDependantObject expressionObj, string dax, DAXProperty prop)
        {
            var dependsOn = new DependsOnList(null);

            ParseExpression(dax, expressionObj, prop, dependsOn);
            return(dependsOn);
        }