public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
            {
                string raw = value as string;

                try
                {
                    string[]   parts    = raw.Split(new char[] { '(' }, 2);            //split using first peren
                    string     main     = parts[0];
                    bool       isTarget = parts[0].ToLower().StartsWith("t");          //target mrefs start with t
                    MeasureRef mref     = new MeasureRef(
                        Int32.Parse(isTarget ? main.Substring(1) : main),
                        isTarget);
                    if (parts.Length > 1)
                    {
                        string[]     paramsRaw  = parts[1].Substring(0, parts[1].Length - 1).Split(',');                //split over commas
                        MeasureRef[] parameters = new MeasureRef[paramsRaw.Length];
                        for (int i = 0; i < paramsRaw.Length; i++)
                        {
                            parameters[i] = (MeasureRef) new Converter().ConvertFrom(paramsRaw[i].Trim());                            //convert each ref
                        }
                        mref.FunctionMeasures = parameters;
                    }

                    return(mref);
                }
                catch (Exception ex)
                {
                    throw new Exception("Invalid measure ref: " + raw, ex);
                }
            }
 private void GetFieldsSQL(MeasureRef measure, int measureIndex, int RangeIndex, out string AggregateFunction, out string HavingString, Dictionary <int, Measure> measuresList, MeasureFilter filter)
 {
     AggregateFunction = BuildAggregateFunction(measure, measuresList);
     HavingString      = AggregateFunction + " IS NOT NULL ";
     try
     {
         if (filter.MeasureIndex == measureIndex && filter.RangeIndex == RangeIndex)
         {
             HavingString += " AND " + AggregateFunction + filter.Operator.ToString() + filter.FilterValue;
         }
     }
     catch (NullReferenceException)
     {
     }
     AggregateFunction = AggregateFunction + " AS 'M" + measureIndex + ".R" + RangeIndex + ".Value'";
 }
        private string BuildAggregateFunction(MeasureRef measure, Dictionary <int, Measure> measuresList)
        {
            Measure m = measuresList[measure.MeasureID];

            if (measure.IsTargetRef)
            {
                m = measuresList[m.TargetMeasureID];
            }

            string AggregateFunction = null;

            AggregateFunction = m.DWH_AggregateFunction.Replace("<DWH_Name>", "a." + m.DWH_Name);
            int             MeasureID = 0;
            MatchCollection matches   = Regex.Matches(m.DWH_AggregateFunction, @"\<[^\>]+\>");

            foreach (Match ma in matches)
            {
                if (ma.Value.ToString().ToLower().Contains("id:"))
                {
                    int Pos = ma.Value.ToString().IndexOf(">");
                    MeasureID = Convert.ToInt32(ma.Value.ToString().Substring(ma.Value.ToString().IndexOf(":") + 1, (Pos - 1) - ma.Value.ToString().IndexOf(":")));
                }
                else if (ma.Value.ToString().ToLower().Contains("param:") && ma.Value.ToString().ToLower().Contains("/"))
                {
                    int Pos1, Pos2;
                    Pos1      = ma.Value.ToString().IndexOf(":");
                    Pos2      = ma.Value.ToString().IndexOf("/");
                    MeasureID = measure.FunctionMeasures[Convert.ToInt32(ma.Value.ToString().Substring(Pos1 + 1, Pos2 - Pos1 - 1)) - 1].MeasureID;
                }
                else if (ma.Value.ToString().ToLower().Contains("param:"))
                {
                    int Pos1, Pos2;
                    Pos1      = ma.Value.ToString().IndexOf(":");
                    Pos2      = ma.Value.ToString().IndexOf(">");
                    MeasureID = measure.FunctionMeasures[Convert.ToInt32(ma.Value.ToString().Substring(Pos1 + 1, Pos2 - Pos1 - 1)) - 1].MeasureID;
                }
                if (MeasureID > 0)
                {
                    Measure _measure = measuresList[MeasureID];
                    AggregateFunction = AggregateFunction.Replace(ma.Value, "a." + _measure.DWH_Name);
                }
            }
            return(AggregateFunction);
        }