I sure hope I can relay this appropriately via writing

I am utlizing code that someone else wrote and don't understand it. In looking through the documentation, etc. and even online, I have struggled with understanding this. I reached out to a developer where i work and she said it was TM1 language and wasn't really able to help me either.

I'll post the code below. It has some comments in it. Where I am getting stuck is in the #3 section... I need the MDX to be dynamic and the "%Brand_Name%" isn't working

Currently, I only know how to really edit this code and dont' understand it.


Here is the code:
DimName = 'SSD MPN';
##################### Subset: Brand #####################
# (1): Unwind the dimension being updated
Index = 1;
ElmCount = DIMSIZ(DimName);
While(Index <= ElmCount);
sElm = DIMNM(DimName,Index);
ChildCount = ELCOMPN(DimName,sElm);
WHILE(ChildCount > 0);
sChildElm = ELCOMP(DimName,sElm,ChildCount);
DIMENSIONELEMENTCOMPONENTDELETE(DimName, sElm, sChildElm );
ChildCount = ChildCount - 1;
END;
Index = Index + 1;
END;
# (2): Add all Level 0 elements to 'Total xxxxx' consolidation
Index = 1;
ElmCount = dimsiz(DimName);
While (index <= ElmCount );
sElm = DIMNM(DimName,Index);
IF(DTYPE(DimName,sElm) @<>'c');
DimensionElementComponentAdd(DimName, 'Total MPN',sElm,1);
ENDIF;
Index = Index + 1;
END;
#
DimName = 'SSD MPN';
# (3): Dynamically add subsets to DimName based on attribute of DimName
Index = 1;
ElmCount = dimsiz(DimName);
While (index < ElmCount );
Brand_Name = attrs(DimName,DIMNM(DimName,Index),'Brand');
SubsetPre = INSRT('Brand', '_', 1);
If(Brand_Name @<>'' );
SubsetName = SubsetPre | Brand_Name;
If( SubsetExists(DimName,SubsetName) = 0);
SubsetCreatebyMDX(SubsetName,Expand('{FILTER( { DRILLDOWNMEMBER( {TM1SubsetBasis()}, {[SSD MPN].[Brand] = "%Brand_Name%"} ) }, [SSD MPN].[Inclusion Flag] = "Yes")},
[SSD MPN].[Brand] = "%Brand_Name%")}'));
ENDIF;
ENDIF;
Index = Index + 1;
END;
# (4): Dynamically add consolidated elements based on attribute of DimName
Index = 1;
ElmCount = dimsiz(DimName);
While (index <= ElmCount );
sElm = DIMNM(DimName,Index);
Brand_Name = attrs(DimName,sElm,'Brand');
SubsetPre = INSRT('Brand', '_', 1);
If( Brand_Name @<>'' );
ConsolName = SubsetPre | Brand_Name;
DimensionElementInsert(DimName,'',ConsolName,'c');
DimensionElementComponentAdd(DimName, ConsolName,sElm,1);
ENDIF;
Index = Index + 1;
END;
Thanks a lot (as always). Appreciate any sort of guidence you can provide.