Doping of materials for thermoelectric applications is widely used nowadays to control the type of conductivity. We report the results of ab-initio calculations aimed at developing the consistent scheme for determining the role of impurities that may change the type of conductivity in two attractive thermoelectric classes of materials. It is demonstrated that alloying of TiNiSn with Cu makes the material of n-type, and alloying with Fe leads to p-type conductivity. Similar calculations for PbTe with small amount of Na substituting for Pb leads to p-type conductivity, while Cl substituting for Te makes PbTe an n-type material. It is shown also that for nano-grained materials the n-type conductivity should be observed. The effect of impurities segregating to the grain boundaries in nano-structured PbTe is also discussed.